Java Server Pages

Moderators: None (Apply to moderate this forum)
Number of threads: 306
Number of posts: 629

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Can I get an example of Java Mail? Posted by teddyj on 9 Jul 2004 at 8:32 AM
Are there any examples of sending an email using Java Mail using a JSP,
Servlet and maybe a java bean? How does SMTP come into play, if at all?

Report
Re: Can I get an example of Java Mail? Posted by dey_swapan on 7 Aug 2004 at 1:51 AM
Go through the following code carefully. This program is developed by me and running. Please don't forget to change the email addresses and smtp server's host,port names before compiling the program.



import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.smtp.SMTPTransport;

public class TextMailSender {

private Transport tr;
private Message msg;
private Session sn;
public void setMessage(String fromAddress,String toAddress,
String subject,String content) throws Exception {
String[] addresses = {toAddress};
setMessage(fromAddress,addresses,subject,content);
}

public void setMessage(String fromAddress,String[] toAddresses,
String subject,String content) throws Exception {

sn = Session.getInstance(System.getProperties());


msg = new MimeMessage(sn);
msg.setFrom(new InternetAddress(fromAddress));
InternetAddress[] toIntAdds = new InternetAddress[toAddresses.length];

for (int i=0;i<toAddresses.length;i++)
toIntAdds[i] = new InternetAddress(toAddresses[i]);

msg.setRecipients(Message.RecipientType.TO,toIntAdds);
msg.setSubject(subject);
msg.setSentDate(new java.util.Date());
msg.setText(content);
}

public void setSMTPServer(String host,int port,
String user,String password) throws Exception{
tr = new SMTPTransport(sn,new URLName(host));
tr.connect(host,port,null,null);
}

public void send() throws Exception{
msg.saveChanges();
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
}

public static void main(String[] args) throws Exception{
TextMailSender tms = new TextMailSender();
tms.setMessage("swapan_java_api_prog@swapan.net.uk",
new String[]{"jitesh.india@mailbox123.com","dey_swapan@swapan2.com"},"test mail","Boss, I am sendingthis mail from my java program. " +
"The from id doesn't exists but you can reply this mail to my official id");
tms.setSMTPServer("localhost",25,null,null);
tms.send();
}
}
Report
Re: Can I get an example of Java Mail? Posted by teddyj on 9 Aug 2004 at 7:26 AM
This message was edited by teddyj at 2004-8-9 7:27:27

: Go through the following code carefully. This program is developed by me and running. Please don't forget to change the email addresses and smtp server's host,port names before compiling the program.
:
:
:
: import javax.mail.Message;
: import javax.mail.Session;
: import javax.mail.Transport;
: import javax.mail.URLName;
: import javax.mail.internet.InternetAddress;
: import javax.mail.internet.MimeMessage;
:
: import com.sun.mail.smtp.SMTPTransport;
:
: public class TextMailSender {
:
: private Transport tr;
: private Message msg;
: private Session sn;
: public void setMessage(String fromAddress,String toAddress,
: String subject,String content) throws Exception {
: String[] addresses = {toAddress};
: setMessage(fromAddress,addresses,subject,content);
: }
:
: public void setMessage(String fromAddress,String[] toAddresses,
: String subject,String content) throws Exception {
:
: sn = Session.getInstance(System.getProperties());
:
:
: msg = new MimeMessage(sn);
: msg.setFrom(new InternetAddress(fromAddress));
: InternetAddress[] toIntAdds = new InternetAddress[toAddresses.length];
:
: for (int i=0;i<toAddresses.length;i++)
: toIntAdds[i] = new InternetAddress(toAddresses[i]);
:
: msg.setRecipients(Message.RecipientType.TO,toIntAdds);
: msg.setSubject(subject);
: msg.setSentDate(new java.util.Date());
: msg.setText(content);
: }
:
: public void setSMTPServer(String host,int port,
: String user,String password) throws Exception{
: tr = new SMTPTransport(sn,new URLName(host));
: tr.connect(host,port,null,null);
: }
:
: public void send() throws Exception{
: msg.saveChanges();
: tr.sendMessage(msg, msg.getAllRecipients());
: tr.close();
: }
:
: public static void main(String[] args) throws Exception{
: TextMailSender tms = new TextMailSender();
: tms.setMessage("swapan_java_api_prog@swapan.net.uk",
: new String[]{"jitesh.india@mailbox123.com","dey_swapan@swapan2.com"},"test mail","Boss, I am sendingthis mail from my java program. " +
: "The from id doesn't exists but you can reply this mail to my official id");
: tms.setSMTPServer("localhost",25,null,null);
: tms.send();
: }
: }
:





Report
Re: Can I get an example of Java Mail? Posted by teddyj on 9 Aug 2004 at 7:30 AM
: This message was edited by teddyj at 2004-8-9 7:27:27
Thank you I have My java Mail working. I will use your example and compare it with mine to see the difference.
Thank you for your time.
: : Go through the following code carefully. This program is developed by me and running. Please don't forget to change the email addresses and smtp server's host,port names before compiling the program.
: :
: :
: :
: : import javax.mail.Message;
: : import javax.mail.Session;
: : import javax.mail.Transport;
: : import javax.mail.URLName;
: : import javax.mail.internet.InternetAddress;
: : import javax.mail.internet.MimeMessage;
: :
: : import com.sun.mail.smtp.SMTPTransport;
: :
: : public class TextMailSender {
: :
: : private Transport tr;
: : private Message msg;
: : private Session sn;
: : public void setMessage(String fromAddress,String toAddress,
: : String subject,String content) throws Exception {
: : String[] addresses = {toAddress};
: : setMessage(fromAddress,addresses,subject,content);
: : }
: :
: : public void setMessage(String fromAddress,String[] toAddresses,
: : String subject,String content) throws Exception {
: :
: : sn = Session.getInstance(System.getProperties());
: :
: :
: : msg = new MimeMessage(sn);
: : msg.setFrom(new InternetAddress(fromAddress));
: : InternetAddress[] toIntAdds = new InternetAddress[toAddresses.length];
: :
: : for (int i=0;i<toAddresses.length;i++)
: : toIntAdds[i] = new InternetAddress(toAddresses[i]);
: :
: : msg.setRecipients(Message.RecipientType.TO,toIntAdds);
: : msg.setSubject(subject);
: : msg.setSentDate(new java.util.Date());
: : msg.setText(content);
: : }
: :
: : public void setSMTPServer(String host,int port,
: : String user,String password) throws Exception{
: : tr = new SMTPTransport(sn,new URLName(host));
: : tr.connect(host,port,null,null);
: : }
: :
: : public void send() throws Exception{
: : msg.saveChanges();
: : tr.sendMessage(msg, msg.getAllRecipients());
: : tr.close();
: : }
: :
: : public static void main(String[] args) throws Exception{
: : TextMailSender tms = new TextMailSender();
: : tms.setMessage("swapan_java_api_prog@swapan.net.uk",
: : new String[]{"jitesh.india@mailbox123.com","dey_swapan@swapan2.com"},"test mail","Boss, I am sendingthis mail from my java program. " +
: : "The from id doesn't exists but you can reply this mail to my official id");
: : tms.setSMTPServer("localhost",25,null,null);
: : tms.send();
: : }
: : }
: :
:
:
:
:
:
:

Report
Re: Can I get an example of Java Mail? Posted by w0lfshad3 on 25 Mar 2009 at 3:01 AM
Great example, i got it working from 1st try. Worked with local mail server but i can't get it to work with gmail:
smtp.gmail.com
465
or yahoo:
smtp.mail.yahoo.com
25



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.