Servlet使用SmtpClient发送邮件需要验证身份
package servlet.mail; import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import sun.net.smtp.SmtpClient; /** * @author shiwx * @since 2011-12-5 * @version 1.0 */ public class MailServlet extends HttpServlet { private static final long serialVersionUID = 6204547780699076040L; public static String MAIL_FROM = "shiwx@asiainfo-linkage.com"; public static String MAIL_TO = "dongbt@asiainfo-linkage.com"; public static String MAIL_HOST = "smtp.asiainfo-linkage.com"; /** * Servlet使用SmtpClient发送邮件 */ @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); PrintWriter out = resp.getWriter(); SmtpClient mailer = new SmtpClient(MAIL_HOST); mailer.from(MAIL_FROM); mailer.to(MAIL_TO); PrintStream ps = mailer.startMessage(); ps.println("From: " + MAIL_FROM); ps.println("To: " + MAIL_TO); ps.println("Subject: " + "MAIL_TITLE"); ps.println("MAIL_BODY"); ps.flush(); ps.close(); mailer.closeServer(); out.println("<h2>Your email has been sent!</h2>"); out.flush(); out.close(); } @Override public void destroy() { super.destroy(); } @Override public void init() throws ServletException { super.init(); } }
Servlet使用SmtpClient发送邮件需要验证身份,
报异常:553 From <shiwx@asiainfo-linkage.com>, message blocked, you are not authorized to send mail, authentication is required.
请问使用SmtpClient如何处理身份验证?下面是代码
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
引用来自“红薯”的答案
建议用
commons-email 包,封装了邮件发送的过程,支持用户认证很方便
发邮件需要用户名和密码认证的
建议用
commons-email 包,封装了邮件发送的过程,支持用户认证很方便