2014-12-03 2 views
1

프로그래밍이 매우 어렵습니다. 약한 설명이 유감입니다. 나는 거의 모든 가능한 답변을 구글에서 시도했다. 오류가 발생하는 이유를 알 수 없습니다.이 코드의 목적은 파일을 첨부하여 전자 메일 주소로 보내는 것입니다. 나는 어떤 이메일 서버도 갖고 있지 않다. 그것이 작동하지 않는 이유 일식 콘솔에서먼저 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

나는 발견했다. Google에서 코드를 내 프로젝트에 복사했기 때문입니다. 코드에서

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 인수 상자의 (X) = 인수 탭에서 "-Dmail.smtp.starttls.enable = true"매개 변수를 설정할 수 있습니다.

실행 단추를 클릭하십시오.

또는 명령 프롬프트에서

:

당신은 자바 프로그램에 매개 변수를 제공하고

자바 -Dmail.smtp.starttls.enable = 사실 <> 된 .java

관련 문제