MailUtils 测试邮件是否发送

Posted 龙芳伟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MailUtils 测试邮件是否发送相关的知识,希望对你有一定的参考价值。

 1 import java.util.Properties;
 2 
 3 import javax.mail.Message;
 4 import javax.mail.Session;
 5 import javax.mail.Transport;
 6 import javax.mail.internet.InternetAddress;
 7 import javax.mail.internet.MimeMessage;
 8 import javax.mail.internet.MimeMessage.RecipientType;
 9 
10 public class MailUtils {
11     private static String smtp_host = "smtp.126.com"; 
12     private static String username = "[email protected]"; 
13     private static String password = "147963qP"; 
14 
15     private static String from = "[email protected]"; // 使用当前账户
16     public static String activeUrl = "http://localhost:8082/bos_fore/customerAction_activeMail";
17 
18     public static void sendMail(String subject, String content, String to) {
19         Properties props = new Properties();
20         props.setProperty("mail.smtp.host", smtp_host);
21         props.setProperty("mail.transport.protocol", "smtp");
22         props.setProperty("mail.smtp.auth", "true");
23         Session session = Session.getInstance(props);
24         Message message = new MimeMessage(session);
25         try {
26             message.setFrom(new InternetAddress(from));
27             message.setRecipient(RecipientType.TO, new InternetAddress(to));
28             message.setSubject(subject);
29             message.setContent(content, "text/html;charset=utf-8");
30             Transport transport = session.getTransport();
31             transport.connect(smtp_host, username, password);
32             transport.sendMessage(message, message.getAllRecipients());
33         } catch (Exception e) {
34             e.printStackTrace();
35             throw new RuntimeException("邮件发送失败...");
36         }
37     }
38 
39     public static void main(String[] args) {
40         sendMail("测试邮件", "你好,大神", "[email protected]");
41     }
42 }

 

以上是关于MailUtils 测试邮件是否发送的主要内容,如果未能解决你的问题,请参考以下文章

javaWeb开发小工具---MailUtils及其单元测试

使用mailutils发邮件

java发送邮件

Postfix 不发送邮件

超实用的php代码片段

我应该如何使用 Outlook 发送代码片段?