2012-03-14 243 views
2

我已经写了这是给例外发送邮件java代码:当我使用的端口号465采用发送电子邮件Gmail帐户

com.sun.mail.smtp.SMTPSendFailedException:530-5.5.1需要身份验证。了解更多530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 l1sm2119061pbe.54

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

String USERNAME = username; 
String PASSWORD = password; 


Properties props = new Properties(); 
props.put("mail.smtp.host", smtpHost); 
if(smtpPort != null){ 
    int SMTP_PORT = Integer.parseInt(smtpPort); 
    props.put("mail.smtp.port", SMTP_PORT); 
} 
props.put("mail.from",from); 
props.put("mail.smtp.starttls.enable", "true"); 
if(auth == 1){ 
    props.put("mail.smtp.auth", "true"); 
} 
props.put("mail.debug", "true"); 

Session session = Session.getInstance(props, new Authenticator() { 
     @Override 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(USERNAME, PASSWORD); 
     } 
}); 

MimeMessage msg = new MimeMessage(session); 
msg.setFrom(); 
msg.setRecipients(Message.RecipientType.TO, to); 
msg.setSubject(subject); 
msg.setSentDate(new Date()); 

Multipart content = new MimeMultipart(); 
MimeBodyPart bodyPart = new MimeBodyPart(); 

bodyPart.setText(htmlBody, "UTF-8"); 
bodyPart.setContent(htmlBody, "text/html"); 

content.addBodyPart(bodyPart); 

if (attachmentPath != null){ 
    String[] a = attachmentPath.split(","); 

    for (int i = 0; i < a.length; i++) { 
      if(a[i] != null) 
     { 
      MimeBodyPart attachmentPart = new MimeBodyPart(); 
      attachmentPart.attachFile(a[i].trim()); 
      content.addBodyPart(attachmentPart); 
     } 
    } 
} 


msg.setContent(content); 

Transport.send(msg); 
return true 
+0

SM_SendEmail(来自;至受试者; htmlBody; attachmentPath; smtpHost; SMTPPORT; SSL; TLS; AUTH;用户名;密码):这是方法 – 2012-03-14 09:15:21

回答

1

代码是不言自明的,这是我在项目中使用的工作代码。希望这可以帮助你。祝你好运。

/** 
GmailSmtpSSL emailNotify = new GmailSmtpSSL(cred[ID], cred[PASS]); 

emailNotify.sendMailTo("self","Testing AlfaDX Gmail module", "Yes it works"); 

**/ 

import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Properties; 

import javax.mail.Authenticator; 
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 GmailSmtpSSL { 

    GmailSmtpSSL(String username,String password) { 
     usern = username; 
     pass = password; 

     setDebugMsg("Setting user name to : "+usern); 
     setDebugMsg("Using given password : "+pass); 

     props = new Properties(); 
     setDebugMsg("Setting smtp server: smtp.gmail.com"); 
     setDebugMsg("Using SSL at port 465 auth enabled"); 

     props.put("mail.smtp.host", "smtp.gmail.com"); 
     props.put("mail.smtp.port", "465"); 
     props.put("mail.smtp.user", usern); 

     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     //props.put("mail.smtp.debug", "true"); 

     props.put("mail.smtp.socketFactory.port", "465"); 
     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.socketFactory.fallback", "false"); 

     SMTPAuthenticator auth = new SMTPAuthenticator(); 
     session = Session.getDefaultInstance(props, auth); 
     session.setDebug(true); 
     setDebugMsg("Session initialization complete"); 
    } 

    public void destroy() { 
     props.clear(); 
     props = null; 
     usern = null; 
     pass = null; 
     session = null; 

    } 

    public void sendMailTo(String to, String sub, String body) 
                throws MessagingException { 

     Calendar currentDate = Calendar.getInstance(); 
     SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss a"); 
     String dateToday = formatter.format(currentDate.getTime()).toLowerCase(); 

     if (to.equals("self")) 
      to = usern; 

     setDebugMsg("Composing message: To "+to); 
     setDebugMsg("Composing message: Subject "+sub); 

     try { 
      Message message = new MimeMessage(session); 
      message.setFrom(new InternetAddress(usern)); 
      message.setRecipients(Message.RecipientType.TO, 
                InternetAddress.parse(to)); 
      message.setSubject("PinguBot: "+dateToday+" "+sub); 
      message.setText(body); 
      setDebugMsg("Attempting to send..."); 
      Transport transport = session.getTransport("smtps"); 

      transport.connect("smtp.gmail.com", 465, usern, pass); 
      Transport.send(message); 
      transport.close(); 
     } 

     catch(MessagingException me) { 
      setDebugMsg(me.toString()); 
      throw new MessagingException(me.toString()); 
     } 
      setDebugMsg("Mail was send successfully"); 
    } 


    public String getDebugMsg() { 
     String msg = new String(debugMsg); 
     debugMsg = " "; 
     return msg; 
    } 
    private static void setDebugMsg(String msg) { 
     debugMsg += msg + "\n"; 
     System.out.println(msg); 
    } 

    private static String debugMsg = ""; 
    private String usern; 
    private String pass; 
    private Session session; 
    private static Properties props; 

    private class SMTPAuthenticator extends Authenticator 
    { 
     public PasswordAuthentication getPasswordAuthentication() 
     { 
      return new PasswordAuthentication(usern, pass); 
     } 
    } 

} 
+0

测试此代码之前。不要担心你在代码中发现的错误。这两个代码是完全不同的。试试看。 – alkber 2012-03-14 09:43:31

+0

感谢buddyyyy .....我为您提供派对......希望您的光明未来...... – 2012-03-14 10:10:59

+0

我接受所有付款选项;-)。开玩笑。祝你好运。 – alkber 2012-03-14 10:15:33

0

当我使用的端口号-1

不足为奇的。 -1不是有效的端口号。当您使用正确的端口号时会发生什么?

当我使用的端口号465

这不是一个标准的SMTP端口两种。

当你使用正确的端口号时会发生什么?

+0

当我使用465然后将其给予相同例外:的com.sun .mail.smtp.SMTPSendFailedException:530-5.5.1需要身份验证。有关详情,请参阅530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 l1sm2119061pbe.54 – 2012-03-14 09:24:31

+0

标准与否,Gmail使用465作为SSL的SMTP端口:http:// support.google.com/mail/bin/answer.py?hl=zh_CN&answer=13287 – 2012-03-14 09:29:01

+0

@EliAcherkan好,所以它应该可以正常工作,只要他使用SSL。 – EJP 2012-03-14 10:48:26

0

使用以下代码验证邮件并使用javax.mail将邮件发送到Gmail服务器。照顾防火墙和您选择的主机端口。

import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.mail.*; 
import javax.mail.internet.*; 
import java.util.*; 

final class MailClient 
{ 
    private class SMTPAuthenticator extends Authenticator 
    { 
     private PasswordAuthentication authentication; 

     public SMTPAuthenticator(String login, String password) 
     { 
      authentication = new PasswordAuthentication(login, password); 
     } 

     @Override 
     protected PasswordAuthentication getPasswordAuthentication() 
     { 
      return authentication; 
     } 
    } 

    public void mail() 
    { 
     try 
     { 
      String from = "xyz.com"; 
      String to = "abc.com"; 
      String subject = "Your Subject."; 
      String message = "Message Text."; 
      String login = "xyz.com"; 
      String password = "password"; 

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

      Authenticator auth = new SMTPAuthenticator(login, password); 

      Session session = Session.getInstance(props, auth); 

      MimeMessage msg = new MimeMessage(session); 

      try 
      { 
       msg.setText(message); 
       msg.setSubject(subject); 
       msg.setFrom(new InternetAddress(from)); 
       msg.addRecipient(Message.RecipientType.TO, 
       new InternetAddress(to)); 
       Transport.send(msg); 
      } 
      catch (MessagingException ex) 
      { 
       Logger.getLogger(MailClient.class.getName()). 
       log(Level.SEVERE, null, ex); 
      } 
     } 
    } 
} 

final public class Main 
{ 
    public static void main(String...args) 
    { 
     new MailClient().mail(); 
    } 
} 
+0

您可以从代码中分离和使用附件,并将其与此代码一起使用。 – Lion 2012-03-14 09:29:47

相关问题