2014-12-03 52 views
1

我在編程方面頗爲新穎,對於弱解釋抱歉。我已經嘗試了幾乎所有可用的答案在谷歌。但無法弄清楚爲什麼錯誤即將到來。此代碼的目的是附加一個文件並將其發送到一個電子郵件地址。我沒有任何電子郵件服務器。在Eclipse控制檯必須首先發出STARTTLS命令。 - gsmtp;使用Gmail帳戶發送帶有附件的電子郵件

import java.util.Properties;  
import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.activation.FileDataSource; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Multipart; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 

public class SendEmailMain { 

public static void main(String[] args) { 

    final String username = "[email protected]"; 
    final String password = "mypassword"; 
    String emailID = "[email protected]"; 

    Properties props = new Properties(); 
    props.put("mail.smtp.starttls.enable", true); 
    props.put("mail.smtp.auth", 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(username)); 
     message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse(emailID)); 
     message.setSubject("Testing Subject"); 
     message.setText("PFA"); 

     MimeBodyPart messageBodyPart = new MimeBodyPart(); 

     Multipart multipart = new MimeMultipart(); 

     messageBodyPart = new MimeBodyPart(); 
     String file = "snap1.jpg"; 
     String fileName = "attachmentName"; 
     DataSource source = new FileDataSource(file); 
     messageBodyPart.setDataHandler(new DataHandler(source)); 
     messageBodyPart.setFileName(fileName); 
     multipart.addBodyPart(messageBodyPart); 

     message.setContent(multipart); 

     System.out.println("Sending"); 

     Transport.send(message); 

     System.out.println("Done"); 

    } catch (MessagingException e) { 
     e.printStackTrace(); 
    } 
} 
} 

錯誤消息

Sending 
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. of6sm3574020lbb.11 - gsmtp 
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388) 
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959) 
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583) 
at javax.mail.Transport.send0(Transport.java:169) 
at javax.mail.Transport.send(Transport.java:98) 
at me.screenful.screenshot.ScreenShotTaker.SendEmailMain.main(SendEmailMain.java:70) 

回答

1

我發現它爲什麼不工作。因爲我將代碼從谷歌複製到我的項目。在代碼中

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

值true不是以逗號分隔。它應該是這樣的

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

如果出現錯誤「530 5.7.0必須首先發出STARTTLS命令。在運行時。
在IDE中:

右鍵單擊項目。點擊運行,然後運行配置。

您可以在VM Arguments框的(X)= Arguments選項卡中設置參數「-Dmail.smtp.starttls.enable = true」。

單擊運行按鈕。

在命令提示符:

你給參數,java程序

的Java -Dmail.smtp.starttls.enable =真<>的.java

相關問題