当前位置: 代码迷 >> Java Web开发 >> javamail 发送邮件成功,但收不到,该如何处理
  详细解决方案

javamail 发送邮件成功,但收不到,该如何处理

热度:1146   发布时间:2016-04-17 14:02:44.0
javamail 发送邮件成功,但收不到
各位大哥,  
在java   application   下运行,   能正常发送邮件,   也能成功收到邮件
但是在web下测试程序,发送邮件成功,却收不到邮件.
这是为什么啊?

我的代码如下:
package   com.venustech.utm30.mgrweb.mail;

import   java.io.File;
import   java.io.UnsupportedEncodingException;
import   java.util.Date;
import   java.util.Properties;

import   javax.mail.Address;
import   javax.mail.Message;
import   javax.mail.MessagingException;
import   javax.mail.internet.InternetAddress;
import   javax.mail.internet.MimeMessage;
import   javax.mail.internet.MimeUtility;

import   org.springframework.mail.MailSendException;
import   org.springframework.mail.javamail.JavaMailSenderImpl;
import   org.springframework.mail.javamail.MimeMessageHelper;

import   com.venustech.utm30.mgrweb.dao.IMail;
import   com.venustech.utm30.mgrweb.dao.pojo.Mailsetting;
import   com.venustech.utm30.mgrweb.dao.pojo.MailsettingDAO;
import   com.venustech.utm30.mgrweb.dto.MailDTO;

/**
  *  
  *   @author   Liao   yeqin
  *  
  */
public   class   MailSender   {

private   String   mailEncoding   =   "GB2312 ";

public   static   void   main(String[]   args)   {
MailSender   send   =   new   MailSender();
MailDTO   mail   =   new   MailDTO();
IMail   imailDao   =   new   MailsettingDAO();
Mailsetting   mailset   =   imailDao.get();
mail.setSmtpServer(mailset.getSmtpServer());
mail.setMailSender(mailset.getSender());
mail.setSmtpUser(mailset.getSmtpUser());
mail.setSmtpPwd(mailset.getSmtpPwd());
String[]   receivers   =   {   "liao0115433@126.com "   };
//   mail.setSmtpUseAuth(mailset.getNeedAuth());
mail.setMailRecipientor(receivers);
mail.setMailTitle( "换种方式做父母today   test:: "   +   new   Date());
mail.setMailContent( "换种方式做父母kdkdkdk ");
String[]   attachments   =   {   " "   };
mail.setAttachments(attachments);
send.sendMail(mail);
}

private   boolean   isEmpty(String   str)   {
return   (str   ==   null   ||   str.equals( " "))   ?   true   :   false;
}

public   void   sendMail(MailDTO   mail)   {

if   (mail   ==   null)   {
return;   //   没有邮件要发送
}

String   smtpHost   =   mail.getSmtpServer();
int   nSmtpPort   =   mail.getPort();
String   mailSender   =   mail.getMailSender();
String   mailUsr   =   mail.getSmtpUser();
String   mailPwd   =   mail.getSmtpPwd();
boolean   smtpUseAuth   =   mail.isSmtpUseAuth();
String[]   mailRecipientors   =   mail.getMailRecipientor();
String   mailCC   =   mail.getCC();
String   mailTitle   =   mail.getMailTitle();
String   mailContent   =   mail.getMailContent();
String[]   attachments   =   mail.getAttachments();

if   (mailRecipientors   ==   null   ||   mailRecipientors.length   ==   0)   {
return;   //   没有邮件要发送
}

JavaMailSenderImpl   senderImpl   =   new   JavaMailSenderImpl();