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.
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(); } }
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
Comments
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();
}
}
: 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();
: }
: }
:
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();
: : }
: : }
: :
:
:
:
:
:
:
smtp.gmail.com
465
or yahoo:
smtp.mail.yahoo.com
25