0

this is my code mention in that box.so please check this code and give me right answer.this code is totally run in my system and run successfully and show build successful but message does not send in mail ids.so please help me.

Thanks in advance

this is my code.

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

        public class Main1
        {
          String  d_email = "xyz@ibm.com",
        d_password = "*******",
        d_host = "blr-outlook.ibm.com",
        d_port  = "25",
        m_to = "xyz@ibm.com",
        m_subject = "Testing",
        m_text = "Hey, this is the testing email using blr-outlook.ibm.com";
           public static void main(String[] args)
          {
     String[] to={"xyz@ibm.com"};
             String[] cc={"xyz@ibm.com"};
           String[] bcc={"xyz@ibm.com"};
    //This is for google
             Main1.sendMail("xyz@ibm.com", "password", "blr-outlook.ibm.com", 
                  "25", "true", "true", 
                  true, "javax.net.ssl.SSLSocketFactory", "false", 
                  to, cc, bcc, 
                  "hi baba don't send virus mails..", 
                  "This is my style...of reply..If u send virus mails..");
         }

         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[] cc, String[] bcc, 
    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",socketFactoryClass);
    }
    if(!"".equals(fallback)) {
        props.put("mail.smtp.socketFactory.fallback", fallback);
    }

    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("blr-outlook.ibm.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)
           {
        return false;
            }
         }

      }
Kapeesh Gupta
  • 9
  • 2
  • 10

1 Answers1

0

There are so many examples available in the net...even tutorialspoint have well explained examples....check it out here.....

http://www.tutorialspoint.com/javamail_api/javamail_api_overview.htm

i have used java mail api for mailing purposes and it works good enough.....i am away from home so i can't share the code with you now....

Edit:

    import java.util.Properties;

    import javax.mail.Message;

    import javax.mail.MessagingException;

    import javax.mail.PasswordAuthentication;

    import javax.mail.Session;

    import javax.mail.Transport;

    import javax.mail.internet.InternetAddress;

    import javax.mail.internet.MimeMessage;


    public class SendMailTLS {

public static void main(String[] args) {

    final String username = "username@gmail.com";
    final String password = "password";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("from-email@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("to-email@gmail.com"));
        message.setSubject("Testing Subject");
        message.setText("Dear Mail Crawler,"
            + "\n\n No spam to my email, please!");

        Transport.send(message);

        System.out.println("Done");


        } catch (MessagingException e) {

    throw new RuntimeException(e);

        }

    }

    }

try this....authentication is not mandatory....and if you are using any kinda app you need to change your gmail account setting to allow access for less secured apps....

Sudershan
  • 425
  • 1
  • 4
  • 17