如何用Java发送电子邮件?

Ste*_*eod 29 java email tomcat servlets

我需要从Tomcat中运行的servlet发送电子邮件.我总是会向同一个收件人发送相同的主题,但内容不同.

使用Java发送电子邮件的简单方法是什么?

有关:

如何使用GMail从Java应用程序发送电子邮件?

Ric*_*dle 31

这是我的代码:

import javax.mail.*;
import javax.mail.internet.*;

// Set up the SMTP server.
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", "smtp.myisp.com");
Session session = Session.getDefaultInstance(props, null);

// Construct the message
String to = "you@you.com";
String from = "me@me.com";
String subject = "Hello";
Message msg = new MimeMessage(session);
try {
    msg.setFrom(new InternetAddress(from));
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
    msg.setSubject(subject);
    msg.setText("Hi,\n\nHow are you?");

    // Send the message.
    Transport.send(msg);
} catch (MessagingException e) {
    // Error.
}
Run Code Online (Sandbox Code Playgroud)

您可以从Sun获取JavaMail库:http://java.sun.com/products/javamail/


Jon*_*Jon 23

使用JavaMail可能有点痛苦.如果您想要一个更简单,更清晰的解决方案,那么请查看JavaMail的Spring包装器.参考文档在这里:

http://static.springframework.org/spring/docs/2.5.x/reference/mail.html

但是,这确实意味着您需要在应用程序中使用Spring,如果这不是一个选项,那么您可以查看另一个开源包装器,例如simple-java-mail:

simplejavamail.org

或者,您可以直接使用JavaMail,但上述两种解决方案是使用Java发送电子邮件的更简单,更简洁的方法.


Ste*_*e K 20

包装Java Mail API的另一个选项是Apache的commons-email.

来自他们的用户指南.

SimpleEmail email = new SimpleEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send();
Run Code Online (Sandbox Code Playgroud)


小智 9

要跟进jon的回复,这是一个使用简单java邮件发送邮件的示例.

这个想法是你不需要知道构成电子邮件的所有技术(嵌套)部分.从这个意义上来说,它很像Apache的commons-email,除了在处理附件和嵌入式图像时,Simple Java Mail比Apache的邮件API更简单.Spring的邮件工具也可以使用,但在使用时有点笨拙(例如它需要一个匿名的内部类),当然你需要依赖Spring,它不仅仅是一个简单的邮件库,因为它是它的基础设计成为IOC解决方案.

简单的Java Mail btw是JavaMail API的包装器.

final Email email = new Email();

email.setFromAddress("lollypop", "lolly.pop@somemail.com"); 
email.setSubject("hey");
email.addRecipient("C. Cane", "candycane@candyshop.org", RecipientType.TO);
email.addRecipient("C. Bo", "chocobo@candyshop.org", RecipientType.BCC); 
email.setText("We should meet up! ;)"); 
email.setTextHTML("<img src='cid:wink1'><b>We should meet up!</b><img src='cid:wink2'>");

// embed images and include downloadable attachments 
email.addEmbeddedImage("wink1", imageByteArray, "image/png");
email.addEmbeddedImage("wink2", imageDatesource); 
email.addAttachment("invitation", pdfByteArray, "application/pdf");
email.addAttachment("dresscode", odfDatasource);

new Mailer("smtp.host.com", 25, "username", "password").sendMail(email);
// or alternatively, pass in your own traditional MailSession object.
new Mailer(preconfiguredMailSession).sendMail(email);
Run Code Online (Sandbox Code Playgroud)


Mau*_*rry 5

我通常在tomcat的server.xml文件的GlobalNamingResources部分定义我的javamail会话,这样我的代码就不依赖于配置参数:

<GlobalNamingResources>
    <Resource name="mail/Mail" auth="Container" type="javax.mail.Session"
              mail.smtp.host="localhost"/>
    ...
</GlobalNamingResources>
Run Code Online (Sandbox Code Playgroud)

我通过JNDI得到了会话:

    Context context = new InitialContext();
    Session sess = (Session) context.lookup("java:comp/env/mail/Mail");

    MimeMessage message = new MimeMessage(sess);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    message.setSubject(subject, "UTF-8");
    message.setText(content, "UTF-8");
    Transport.send(message);
Run Code Online (Sandbox Code Playgroud)