Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Post trying to sendin mail with java mail api

    hi i m trying to sending mail but its cannot run with the user id of rediffmail id ,yahoo mail or etc....................pls help me its works with gmail id only
    the error comes could not connet to "smtp.server.com"


    import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.*;

    public class mail1
    {
    String d_email = "akhil.sri@gmsil.in",
    d_password = "******",
    d_host = "smtp.gmail.com",
    d_port = "587",
    m_to = "akhil_4u47@rediffmail.com",
    m_subject = "Testing",
    m_text = "Hey, this is the testing email using smtp.gmail.com.";
    public static void main(String[] args)
    {
    String[] to={"akhil_4u47@rediffmail.com"};
    // String[] cc={"XXX@yahoo.com"};
    // String[] bcc={"XXX@yahoo.com"};
    //This is for google
    mail1.sendMail("akhil.sri@gmail.com",
    "password",
    "smtp.technolife.com",
    "587",
    "true",
    "true",
    true,

    "javax.net.ssl.SSLSocketFactory",
    "false",
    to,
    "hiiiiiiiiiii",
    "hiii this is akhil sening mail via smtp and java mail api");
    }

    public synchronized static boolean sendMail(
    String userName,
    String passWord,
    String host,
    String port,
    String starttls,
    String auth,
    boolean debug,
    String socketFactoryClass,
    String fallback,
    String[] to,
    String subject,
    String text){
    Properties props = new Properties();
    //Properties props=System.getProperties();
    props.put("mail.smtp.user", userName);
    props.put("mail.smtp.host", host);
    if(!"".equals(port))
    props.put("mail.smtp.port", port);
    if(!"".equals(starttls))
    props.put("mail.smtp.starttls.enable",starttls);
    props.put("mail.smtp.auth", auth);
    if(debug){
    props.put("mail.smtp.debug", "true");
    }else{
    props.put("mail.smtp.debug", "false");
    }
    if(!"".equals(port))
    props.put("mail.smtp.socketFactory.port", port);
    if(!"".equals(socketFactoryClass))
    props.put("mail.smtp.socketFactory.class",socketFa ctoryClass);
    if(!"".equals(fallback))
    props.put("mail.smtp.socketFactory.fallback", fallback);
    System.out.println("properties");
    try
    {
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);
    MimeMessage msg = new MimeMessage(session);
    msg.setText(text);
    msg.setSubject(subject);
    msg.setFrom(new InternetAddress("akhil_4u47@rediffmail.com"));
    for(int i=0;i<to.length;i++){
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
    }
    // for(int i=0;i<cc.length;i++){
    // msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
    // }
    // for(int i=0;i<bcc.length;i++){
    // msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
    // }
    msg.saveChanges();
    Transport transport = session.getTransport("smtp");
    transport.connect(host, userName, passWord);
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();
    return true;
    }
    catch (Exception mex)
    {
    //mex.printStackTrace();
    System.out.println("error"+mex);
    return false;
    }
    }

    }
    thanks in advance
    Last edited by akhilsrivastava; 04-04-2012 at 09:54 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: trying to sendin mail with java mail api

    Look around the site FAQs and discover how to post code so that it retains its formatting.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. problem with sending mail usin javaX.mail api
    By sandeepsai39 in forum New To Java
    Replies: 4
    Last Post: 11-25-2009, 05:37 AM
  2. Replies: 2
    Last Post: 07-25-2009, 09:36 AM
  3. Replies: 2
    Last Post: 06-04-2009, 01:42 PM
  4. Replies: 2
    Last Post: 05-05-2008, 06:01 AM
  5. Retreiving of mail body using mail number
    By chandu.v09 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-13-2008, 02:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •