Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Can I get an example of Java Mail?

teddyjteddyj Posts: 37Member
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

  • dey_swapandey_swapan Posts: 4Member
    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();
    }
    }
  • teddyjteddyj Posts: 37Member
    [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();
    : }
    : }
    :





  • teddyjteddyj Posts: 37Member
    : [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();
    : : }
    : : }
    : :
    :
    :
    :
    :
    :
    :

  • w0lfshad3w0lfshad3 Posts: 1Member
    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.