저는 Java EE의 새로운 기능이며, 프로젝트를 위해 이메일을 보내야합니다. 누구나 단계와 코드를 입력 할 수 있다면 감사하게 생각합니다!EJB를 사용하여 이메일을 보내는 방법
-8
A
답변
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();
}
}
관련 문제
- 1. PHP를 사용하여 이메일을 보내는 방법
- 2. PHP를 사용하여 이메일을 보내는 방법
- 3. PHP에서 이메일을 보내는 방법
- 4. grails에서 이메일을 보내는 방법
- 5. nodejs에서 이메일을 보내는 방법
- 6. 자동으로 이메일을 보내는 방법
- 7. 이메일을 보내는 방법
- 8. 이메일을 보내는 방법?
- 9. codeiginter에서 이메일을 보내는 방법
- 10. 앱에서 이메일을 보내는 방법
- 11. C에서 이메일을 보내는 방법
- 12. JS 또는 JQUERY를 사용하여 이메일을 보내는 방법 MAILTO를 사용하여 이메일을 보내는 방법
- 13. ActionMailer를 사용하여 AWS SES로 이메일을 보내는 방법
- 14. mailgun PHP 코드를 사용하여 이메일을 보내는 방법
- 15. 맞춤 이메일 템플릿을 사용하여 이메일을 보내는 방법
- 16. PHP를 사용하여 여러 개의 이메일을 보내는 방법
- 17. PHP를 사용하여 정기적으로 이메일을 보내는 방법
- 18. Python을 사용하여 간단한 이메일을 보내는 방법
- 19. phpmailer를 사용하여 여러 주소로 이메일을 보내는 방법
- 20. phpmailer를 사용하여 웹 서버에서 이메일을 보내는 방법
- 21. infusionsoft api를 사용하여 이메일을 보내는 방법
- 22. Laravel 이벤트를 사용하여 이메일을 보내는 방법?
- 23. smtp와 Gmail을 사용하여 안드로이드에서 이메일을 보내는 방법
- 24. Pl/sql에서 이메일을 보내는 방법
- 25. 로컬 서버에서 이메일을 보내는 방법
- 26. Facebook 앱으로 이메일을 보내는 방법
- 27. 아이폰 코드에서 이메일을 보내는 방법
- 28. 접미사 : 사용자에게 이메일을 보내는 방법
- 29. Postfix - 보내는 이메일을 처리하는 방법?
- 30. 프레스코에서 이메일을 보내는 방법 4
확실히 "자바 이메일"로 검색하면 결과가 나타납니다. – millimoose
막혔다면 무엇을 시도 했습니까? –
나는 이것을 시도하고 오류가 발생했습니다 : SMTP 호스트에 연결할 수 없습니다 : smtp.topnet.tn, 포트 : 25, 응답 : 421 –