2013-02-24 4 views
-8

저는 Java EE의 새로운 기능이며, 프로젝트를 위해 이메일을 보내야합니다. 누구나 단계와 코드를 입력 할 수 있다면 감사하게 생각합니다!EJB를 사용하여 이메일을 보내는 방법

+2

확실히 "자바 이메일"로 검색하면 결과가 나타납니다. – millimoose

+2

막혔다면 무엇을 시도 했습니까? –

+0

나는 이것을 시도하고 오류가 발생했습니다 : SMTP 호스트에 연결할 수 없습니다 : smtp.topnet.tn, 포트 : 25, 응답 : 421 –

답변

2

다음 코드는 Google SMTP 서버와 잘 작동합니다. Google 사용자 이름과 비밀번호를 제공해야합니다.

import com.sun.mail.smtp.SMTPTransport; 
import java.security.Security; 
import java.util.Date; 
import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class GoogleMail { 
    private GoogleMail() { 
    } 

    /** 
    * Send email using GMail SMTP server. 
    * 
    * @param username GMail username 
    * @param password GMail password 
    * @param recipientEmail TO recipient 
    * @param title title of the message 
    * @param message message to be sent 
    * @throws AddressException if the email address parse failed 
    * @throws MessagingException if the connection is dead or not in the connected state or if the message is not a MimeMessage 
    */ 
    public static void Send(final String username, final String password, String recipientEmail, String title, String message) throws AddressException, MessagingException { 
     GoogleMail.Send(username, password, recipientEmail, "", title, message); 
    } 

    /** 
    * Send email using GMail SMTP server. 
    * 
    * @param username GMail username 
    * @param password GMail password 
    * @param recipientEmail TO recipient 
    * @param ccEmail CC recipient. Can be empty if there is no CC recipient 
    * @param title title of the message 
    * @param message message to be sent 
    * @throws AddressException if the email address parse failed 
    * @throws MessagingException if the connection is dead or not in the connected state or if the message is not a MimeMessage 
    */ 
    public static void Send(final String username, final String password, String recipientEmail, String ccEmail, String title, String message) throws AddressException, MessagingException { 
     Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 
     final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; 

     // Get a Properties object 
     Properties props = System.getProperties(); 
     props.setProperty("mail.smtps.host", "smtp.gmail.com"); 
     props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); 
     props.setProperty("mail.smtp.socketFactory.fallback", "false"); 
     props.setProperty("mail.smtp.port", "465"); 
     props.setProperty("mail.smtp.socketFactory.port", "465"); 
     props.setProperty("mail.smtps.auth", "true"); 

     /* 
     If set to false, the QUIT command is sent and the connection is immediately closed. If set 
     to true (the default), causes the transport to wait for the response to the QUIT command. 

     ref : http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html 
       http://forum.java.sun.com/thread.jspa?threadID=5205249 
       smtpsend.java - demo program from javamail 
     */ 
     props.put("mail.smtps.quitwait", "false"); 

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

     // -- Create a new message -- 
     final MimeMessage msg = new MimeMessage(session); 

     // -- Set the FROM and TO fields -- 
     msg.setFrom(new InternetAddress(username + "@gmail.com")); 
     msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false)); 

     if (ccEmail.length() > 0) { 
      msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false)); 
     } 

     msg.setSubject(title); 
     msg.setText(message, "utf-8"); 
     msg.setSentDate(new Date()); 

     SMTPTransport t = (SMTPTransport)session.getTransport("smtps"); 

     t.connect("smtp.gmail.com", username, password); 
     t.sendMessage(msg, msg.getAllRecipients());  
     t.close(); 
    } 
} 
+0

m 사용자 이름과 암호는 어디에 두어야합니까 ?? 어느 선 에서요? –

+0

이것들은'Send()'메서드를 호출 할 때 전달하는 매개 변수입니다. – syb0rg

+0

나는 당신이 새로운 사람인 것으로 나타났습니다. 귀하의 질문에 대한 대답이 있으면 [대답을 수락하십시오] (http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)하십시오. – syb0rg

관련 문제