/* Some SMTP servers require a username and password authentication before you can use their Server for Sending mail. This is most common with couple of ISP's who provide SMTP Address to Send Mail. This Program gives any example on how to do SMTP Authentication (User and Password verification) This is a free source code and is provided as it is without any warranties and it can be used in any your code for free. Author : Sudhir Ancha */ import javax.mail.*; import javax.mail.internet.*; import java.util.*; import java.io.*; /* To use this program, change values for the following three constants, SMTP_HOST_NAME -- Has your SMTP Host Name SMTP_AUTH_USER -- Has your SMTP Authentication UserName SMTP_AUTH_PWD -- Has your SMTP Authentication Password Next change values for fields emailMsgTxt -- Message Text for the Email emailSubjectTxt -- Subject for email emailFromAddress -- Email Address whose name will appears as "from" address Next change value for "emailList". This String array has List of all Email Addresses to Email Email needs to be sent to. Next to run the program, execute it as follows, SendMailUsingAuthentication authProg = new SendMailUsingAuthentication(); */ public class SendMailUsingAuthentication { private static final String SMTP_HOST_NAME = "myserver.smtphost.com"; private static final String SMTP_AUTH_USER = "myusername"; private static final String SMTP_AUTH_PWD = "mypwd"; private static final String emailMsgTxt = "Online Order Confirmation Message. Also include the Tracking Number."; private static final String emailSubjectTxt = "Order Confirmation Subject"; private static final String emailFromAddress = "sudhir@javacommerce.com"; // Add List of Email address to who email needs to be sent to private static final String[] emailList = {"mark@yahoo.com", "robin@javacommerce.com"}; public static void main(String args[]) throws Exception { SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication(); smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress); System.out.println("Sucessfully Sent mail to All Users"); } 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_HOST_NAME); props.put("mail.smtp.auth", "true"); Authenticator auth = new SMTPAuthenticator(); Session session = Session.getDefaultInstance(props, auth); 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); // Setting the Subject and Content Type msg.setSubject(subject); msg.setContent(message, "text/plain"); Transport.send(msg); } /** * SimpleAuthenticator is used to do simple authentication * when the SMTP server requires it. */ private class SMTPAuthenticator extends javax.mail.Authenticator { public PasswordAuthentication getPasswordAuthentication() { String username = SMTP_AUTH_USER; String password = SMTP_AUTH_PWD; return new PasswordAuthentication(username, password); } } }
作者:许强1. 本博客中的文章均是个人在学习和项目开发中总结。其中难免存在不足之处 ,欢迎留言指正。 2. 本文版权归作者和博客园共有,转载时,请保留本文链接。
1. 本博客中的文章均是个人在学习和项目开发中总结。其中难免存在不足之处 ,欢迎留言指正。 2. 本文版权归作者和博客园共有,转载时,请保留本文链接。
当前访问量:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?