使用java mail api发送邮件

时间:2012-09-19 14:03:59

标签: java javamail

我正在尝试从我的Java类发送邮件,但不断收到MessangingException。 我认为它与认证问题有关,但我无法弄清楚问题。

以下是异常的堆栈跟踪。

Sep 19, 2012 7:27:30 PM me.uni.sushilkumar.geodine.util.Mailer send
SEVERE: null
javax.mail.SendFailedException: Sending failed;
  nested exception is: 
    javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first.     ko8sm1883776pbc.40

    at javax.mail.Transport.send0(Transport.java:219)
at javax.mail.Transport.send(Transport.java:81)
at me.uni.sushilkumar.geodine.util.Mailer.send(Mailer.java:61)
at me.uni.sushilkumar.geodine.util.Mailer.main(Mailer.java:73)

我使用以下代码发送消息

public class Mailer {
private Session session;
private final String username;
private final String password;
public Mailer(String username,String password)
{
    this.username=username;
    this.password=password;
    init(username,password);
}

public final void init(String username,String 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.getInstance(props, new Authenticator()
    {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(Mailer.this.username,Mailer.this. password);
        }
    });

}

public boolean send(String recipient,String subject,String body) 
{
    boolean status=false;
    try {

        Message message=new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
        message.setSubject(subject);
        message.setText(body);
        Transport.send(message);
        status=true;

    } catch (MessagingException ex) {
        Logger.getLogger(Mailer.class.getName()).log(Level.SEVERE, null, ex);
    }
    return status;
}

public static void main(String[] args)
{
    Mailer mailer=new Mailer("example@example.com","password");
    boolean status=mailer.send("0120sushil@gmail.com", "Demo Mail", "This is a demo message");
}

}

有人能告诉我这段代码有什么问题吗?

3 个答案:

答案 0 :(得分:1)

请检查这个SO答案 - 他们指定一个套接字工厂来显式使用SSL套接字,您可能需要:

Using JavaMail with TLS

这可能足以让你开始运作。

答案 1 :(得分:0)

只需复制并粘贴此代码,然后放置您的电子邮件信息,例如电子邮件地址和密码。

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

public class SendEmail
{
public static void sendEmail(String toEmail,String subject,String  message)
{
    try
    {
        String fromEmail="yourmail@gmail.com";
        String username="yourmail@gmail.com";
        String password="yourpassword";

        Properties props= System.getProperties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.put( "mail.smtp.ssl.enable", "true");

        Session mailSession = Session.getDefaultInstance(props, null);
        mailSession.setDebug(true);

        Message mailMessage=new MimeMessage(mailSession);
        mailMessage.setFrom(new InternetAddress(fromEmail));
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
        mailMessage.setContent(message, "text/plain");
        mailMessage.setSubject(subject);

        Transport transport=mailSession.getTransport("smtp");
        transport.connect("smtp.gmail.com",username,password);
        transport.sendMessage(mailMessage, mailMessage.getAllRecipients());

    }catch (Exception ex) {
       ex.printStackTrace();
     }
   }
 }

答案 2 :(得分:-1)

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 Send_Mail_Dao

{

private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

   public static boolean Send_Mail(String to,String subject,String mess)

   {

        final String username = "qamarislam.comp@gmail.com";

        final String password = "password";

                boolean flag=false;

        Properties props = new Properties();

        props.put("mail.smtp.host", "smtp.gmail.com");

        props.put("mail.smtp.auth", "true");

        props.put("mail.debug", "true");

        props.put("mail.smtp.port", "465");

        props.put("mail.smtp.socketFactory.port", "465");

        props.put("mail.smtp.socketFactory.class", SSL_FACTORY);

        props.put("mail.smtp.socketFactory.fallback", "false");

        Session session = Session.getInstance(props,new javax.mail.Authenticator() 

                  {
                     @Override
                protected PasswordAuthentication getPasswordAuthentication()
                        {
                   return new PasswordAuthentication(username, password);
                }
          }
          );

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("qamarislam.comp@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to));
            message.setSubject(subject);
            message.setText(mess);

            Transport.send(message);
                            flag=true;
            System.out.println("Done");

            } 
        catch (MessagingException e) 
        {
            throw new RuntimeException(e);
        }
                return flag;
    }
}