Can I get an example of Java Mail?

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?

Comments

  • 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();
    }
    }
  • [b][red]This message was edited by teddyj at 2004-8-9 7:27:27[/red][/b][hr]
    : 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();
    : }
    : }
    :





  • : [b][red]This message was edited by teddyj at 2004-8-9 7:27:27[/red][/b][hr]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();
    : : }
    : : }
    : :
    :
    :
    :
    :
    :
    :

  • 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
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories