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();
if (isEmpty(smtpHost) == false)
senderImpl.setHost(smtpHost);
senderImpl.setPort(nSmtpPort);
if (isEmpty(mailUsr) == false)
senderImpl.setUsername(mailUsr);
if (isEmpty(mailPwd) == false) {
senderImpl.setPassword(mailPwd);
smtpUseAuth = true;
}
if (smtpUseAuth == true) {
Properties props = new Properties();
props.put( "mail.smtp.auth ", "true ");
senderImpl.setJavaMailProperties(props);
}
MimeMessage mailMessage = senderImpl.createMimeMessage();
try {
MimeMessageHelper msgHelper = new MimeMessageHelper(mailMessage,
true, mailEncoding);
// 设置发件人
if (isEmpty(mailSender) == false)
msgHelper.setFrom(mailSender);
// 设置收件人
msgHelper.setTo(mailRecipientors);
// 设置抄送人
if (isEmpty(mailCC) == false)
msgHelper.setCc(mailCC);
// 设置邮件主题
if (isEmpty(mailTitle) == false)
msgHelper.setSubject(mailTitle);
// 设置邮件内容
if (isEmpty(mailContent) == false)
msgHelper.setText(mailContent);
// 遍历数组 attachments, 其中attachments中保存的附件的路径
if (attachments != null && attachments.length > 0) {
for (int idx = 0; idx < attachments.length; idx++) {
// 添加附件
File attachmentFile = new File(attachments[idx]);
System.out.println( "file path::: "
+ attachmentFile.getAbsolutePath());
if (attachmentFile != null && attachmentFile.exists()) {
try {
String attachmentFileName = MimeUtility
.encodeText(attachmentFile.getName(),
mailEncoding, "B ");
msgHelper.addAttachment(attachmentFileName,
attachmentFile);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
}
senderImpl.send(mailMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
}
[解决办法]
直接使用POP协议从命令行连接POP Server看看Mail是不是在服务器上。
我用Java.mail包来发送没有问题的。也是在Web程序中。
[解决办法]
关注
[解决办法]
是不是ie安全级别的问题啊,你把安全级别调到最低试试