javamail 利用 sendmail 发送邮件

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cb_121/article/details/4663440

今天需求是系统利用自己的smtp服务器发送邮件,我们的系统架构建立在appfuse基础上的,实际上是利用spring mail对javamail的封装调用,以前一直用的是第三方的smtp服务。

 

开始考虑postfix,口碑好,搞了半天,架设好了,需要域名mx记录支持,我们仅仅是测试使用,没有域名用例测试,没有搞好!

但我们的机器上的sendmail可以向外发送邮件的。

 

于是把原来的配置修改了下,结果javamail就可以调用sendmail当smtp使用了,

修改如下:

 

 

mail.debug=false

mail.transport.protocol=smtp

 

mail.host=localhost

mail.smtp.auth=false

 

mail.username=

mail.password=

 

 

可以这样理解,sendmail也可以当smtp服务器的,只是默认没有安装验证模块。

展开阅读全文

javamail利用servlet发送邮件

04-07

最近学习JAVAMAIL,在学习利用tomcat中的对象来实现web的发送邮件时,我的代码就错误啦,错误如下:rn[code=Java]javax.naming.NamingException: Cannot create resource instance at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:132) at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304) at org.apache.naming.NamingContext.lookup(NamingContext.java:792) at org.apache.naming.NamingContext.lookup(NamingContext.java:139) at org.apache.naming.NamingContext.lookup(NamingContext.java:780) at org.apache.naming.NamingContext.lookup(NamingContext.java:152) at cn.itcast.javamail3.web.mail.servlets.SendMailServlet.doPost(SendMailServlet.java:55) at cn.itcast.javamail3.web.mail.servlets.SendMailServlet.doGet(SendMailServlet.java:34) at javax.servlet.http.HttpServlet.service(HttpServlet.java:689) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:743) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684) at java.lang.Thread.run(Thread.java:619) rn[/code]rn我的mail.jar是放在Tomcat 5.5\common\lib,而视频说的是要放在Tomcat/中的WEB-INF\lib下,我找不大这样的地方,上网搜索说放在那也是一样的,不知这没错吧?rn我的代码是跟着视频写的,视频是有成功的,但我这就不行啦,rn try rn //Demo2.main(new String[]);rn rn Context initCtx = new InitialContext();rn Context envCtx = (Context) initCtx.lookup("java:comp/env");rn Session session = (Session) envCtx.lookup("mail/Dog");rnrn Message message = new MimeMessage(session);rn message.setFrom(new InternetAddress("345345346@qq.com"));rn InternetAddress to[] = new InternetAddress[1];rn to[0] = new InternetAddress("234234234@qq.com");rn message.setRecipients(Message.RecipientType.TO, to);rn message.setSubject("ha");rn message.setText("test");rn //Transport.send(message);rn Transport transport = session.getTransport();rn transport.connect("smtp.qq.com", "12356432", "sdcsdcsc");rn transport.sendMessage(message, to);rn transport.close();rn response.getWriter().print("ok!");rn catch (Exception e) rn // TODO Auto-generated catch blockrn e.printStackTrace(response.getWriter());rn rn rnrn这是我的代码,我用的是MyEclipse 6.0和mySQL,网上说会有兼容问题,现在我的这代码还没涉及数据库,应该还没涉及所谓的兼容问题吧,网上搜还有说是activation.jar的却少,不知是不是呢?上网下了1个,不知道放哪,之后就如mail.jar一样放在tomcat中,但还是那个错误,苦思冥想都不知是哪理错了,所以就来请问个位大侠, 论坛

利用JavaMail发送邮件问题??

08-09

我有一个程序,目的是发送邮件,可是执行总出现异常,恳请大虾指点。异常如下:rnjavax.mail.SendFailedException: Sending failed;rn nested exception is:rn javax.mail.MessagingException: 553 You are not authorized to send mail arns >, authentication is requiredrnrn at javax.mail.Transport.send0(Transport.java:218)rn at javax.mail.Transport.send(Transport.java:80)rn at SimpleSender.send(SimpleSender.java:41)rn at SimpleSender.main(SimpleSender.java:19)rn源代码如下:rnimport javax.mail.*;rnimport javax.mail.internet.*;rnimport java.util.*;rnrnpublic class SimpleSenderrnrnrn public static void main(String args[])rn rn tryrn rn String smtpServer=args[0];rn String to=args[1];rn String from=args[2];rn String subject=args[3];rn String body=args[4];rn send(smtpServer, to, from, subject, body);rn rn catch (Exception ex)rn rn System.out.println("Usage: java com.lotontech.mail.SimpleSender"+" smtpServer toAddress fromAddress subjectText bodyText");rn rn System.exit(0);rn rn public static void send(String smtpServer, String to, String from, String subject, String body)rn rn tryrn rn Properties props = System.getProperties();rn props.put("mail.smtp.host", smtpServer);rn Session session = Session.getDefaultInstance(props, null);rn Message msg = new MimeMessage(session);rn msg.setFrom(new InternetAddress(from));rn msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));rn msg.setSubject(subject);rn msg.setText(body);rn msg.setHeader("X-Mailer", "LOTONtechEmail");rn msg.setSentDate(new Date());rn Transport.send(msg);rn System.out.println("Message sent OK.");rn rn catch (Exception ex)rn rn ex.printStackTrace();rn rn rn 论坛

没有更多推荐了,返回首页