邮件发送代码片段
2017-07-28 16:36
204 查看
import javax.mail.*; import javax.mail.internet.*; import java.util.*; public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException { boolean debug = false; //Set the host smtp address Properties props = new Properties(); props.put("mail.smtp.host", "smtp.example.com"); // create some properties and get the default Session Session session = Session.getDefaultInstance(props, null); session.setDebug(debug); // create a message Message msg = new MimeMessage(session); // set the from and to address InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { addressTo[i] = new InternetAddress(recipients[i]); } msg.setRecipients(Message.RecipientType.TO, addressTo); // Optional : You can also set your custom headers in the Email if you Want msg.addHeader("MyHeaderName", "myHeaderValue"); // Setting the Subject and Content Type msg.setSubject(subject); msg.setContent(message, "text/plain"); Transport.send(msg); }
相关文章推荐
- 工作代码片段-svn提交并发送邮件
- golang 邮件发送代码片段
- 发送邮件代码片段
- PHPMailer 发送邮件示例代码
- PHP邮件发送类(修改网上代码,亲测可用)
- 使用Java代码发送SMTP邮件
- 偶用的一段信息和邮件的发送代码
- (转载)iOS开发之邮件发送代码
- 发送邮件常见出错代码
- .net通过代码发送邮件
- Python之简单的SMTP发送邮件详细教程附代码
- java代码发送邮件
- Java代码实现发送邮件
- JSP实现发送带有附件的邮件代码
- 使用phantomjs实现highcharts等报表通过邮件发送(本文仅提供完整解决方案和实现思路,完全照搬不去整理代码无法马上得到效果)
- asp.net代码怎么自动发送邮件?
- .net2.0邮件发送代码
- ASP.NET中常用功能代码总结——发送邮件篇
- java代码实现发送邮件