javamail发信,smtp是163,发给qq,过程没报错,但运行完成之后163的已发送邮件和qq的收件箱都没有邮件显示
package mail;import java.util.Properties;import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;public class HtmlMessageSender {
String protocol="smtp";
String from="***@163.com";
String to="***@qq.com";
// String cc="***@126.com";
// String bcc="***@yahoo.com.cn";
String subject="HTML测试";
String body="<a href='http://www.163.com'>163.com</a><br><img src='cid:start-process.gif'/>";
public static void main(String[] args) throws Exception{
String server="smtp.163.com";
String user="***";
String pass="***";
HtmlMessageSender sender=new HtmlMessageSender();
Session session=sender.createSession();
MimeMessage message=sender.createMessage(session);
Transport transport=session.getTransport();
transport.connect(server, user, pass);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
}
private MimeMessage createMessage(Session session) throws Exception{
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
// message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
// message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));
message.setSubject(subject);
MimeMultipart multipart=new MimeMultipart("related");
MimeBodyPart htmlBodyPart=new MimeBodyPart();
htmlBodyPart.setContent(body, "text/html ;charset=gb2312");
multipart.addBodyPart(htmlBodyPart);
MimeBodyPart gifBodyPart=new MimeBodyPart();
FileDataSource fds=new FileDataSource("c:\\start-process.gif");
gifBodyPart.setDataHandler(new DataHandler(fds));
gifBodyPart.setContentID("start-process.gif");
multipart.addBodyPart(gifBodyPart);
message.setContent(multipart);
message.saveChanges();
return message;
}
private Session createSession() {
Properties props=new Properties();
props.setProperty("mail.transport.protocol", protocol);
props.setProperty("mail.smtp.auth", "true");
Session session=Session.getInstance(props);
session.setDebug(true);
return session;
}}
package mail;import java.util.Properties;import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;public class HtmlMessageSender {
String protocol="smtp";
String from="***@163.com";
String to="***@qq.com";
// String cc="***@126.com";
// String bcc="***@yahoo.com.cn";
String subject="HTML测试";
String body="<a href='http://www.163.com'>163.com</a><br><img src='cid:start-process.gif'/>";
public static void main(String[] args) throws Exception{
String server="smtp.163.com";
String user="***";
String pass="***";
HtmlMessageSender sender=new HtmlMessageSender();
Session session=sender.createSession();
MimeMessage message=sender.createMessage(session);
Transport transport=session.getTransport();
transport.connect(server, user, pass);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
}
private MimeMessage createMessage(Session session) throws Exception{
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
// message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
// message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));
message.setSubject(subject);
MimeMultipart multipart=new MimeMultipart("related");
MimeBodyPart htmlBodyPart=new MimeBodyPart();
htmlBodyPart.setContent(body, "text/html ;charset=gb2312");
multipart.addBodyPart(htmlBodyPart);
MimeBodyPart gifBodyPart=new MimeBodyPart();
FileDataSource fds=new FileDataSource("c:\\start-process.gif");
gifBodyPart.setDataHandler(new DataHandler(fds));
gifBodyPart.setContentID("start-process.gif");
multipart.addBodyPart(gifBodyPart);
message.setContent(multipart);
message.saveChanges();
return message;
}
private Session createSession() {
Properties props=new Properties();
props.setProperty("mail.transport.protocol", protocol);
props.setProperty("mail.smtp.auth", "true");
Session session=Session.getInstance(props);
session.setDebug(true);
return session;
}}
解决方案 »
- java代码版本不兼容,出来“使用了未经检查或不安全的操作”
- 菜鸟的一个疑问(java)!请求解答。
- 如何刷新JLabel控件?
- 初学者,想寻一些java例程,,谢谢
- 用JAVA实现检测邮件箱的新邮件,如果附件有文件,把它读出来的程序,希望各位高手给D意见???
- 急:一个日期、星期的算法问题!!
- 请问这是什么错误:Exception in thread "main" java.lang.NoClassDefFoundError
- 急,java如何书写三对引号?
- 求救关于WEB报表的BEAN?多谢
- 高分求助:Java打开大图像(4000×4000)竟然Out of memory
- 求出输入的字符串中相同的字符的个数
- 怎么在oracle中一次性插入多条数据,哪个高手来帮一下
Java邮件开发 http://wenku.baidu.com/view/5c9f311910a6f524ccbf8573.htmljava邮件收发程序 http://wenku.baidu.com/view/0c7bc63043323968011c928b.htmljava邮件系统设计 http://wenku.baidu.com/view/fcbba566f5335a8102d22091.htmlJAVA邮件服务API详解 http://wenku.baidu.com/view/dc53fd5c3b3567ec102d8a1c.html张孝祥Java邮件开发详解 http://wenku.baidu.com/view/ac4d2dcf05087632311212fe.html读书笔记《Java邮件开发详解》http://wenku.baidu.com/view/b995a74acf84b9d528ea7a8b.html希望对你有帮助
MultiPartEmail email = new MultiPartEmail();
email.setCharset("UTF-8");
email.setHostName("mail.xxx.com"); //设置发邮件的smtp发送服务器
email.setAuthentication("***", "***"); //账号,密码
try {
email.addTo(***); //收件人
email.setFrom(***); //发件人
email.setSubject(***); //主题
email.setMsg(***); //正文
if(file != null)
{
EmailAttachment attachment = new EmailAttachment(); //附件
attachment.setPath(file.getPath()); //附件的路径
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setName(file.getName()); //附件名字
email.attach(attachment); //添加附件
}
email.send();
原创 java邮件开发 http://blog.csdn.net/py330316117/archive/2010/12/31/6109608.aspx
以上两篇是我开发google邮箱的步骤和一些心得,是最新的,你可以参考下。
经测试,如果邮件内容是简单的文本的话,从163是可以发到QQ上的;当邮件内容是复杂的MIME邮件时,从163上发不到QQ上,但从QQ上是可以发到163上的,您知道这是怎么回事吗
经测试,如果邮件内容是简单的文本的话,从163是可以发到QQ上的;当邮件内容是复杂的MIME邮件时,从163上发不到QQ上,但从QQ上是可以发到163上的,您知道这是怎么回事吗
经测试,如果邮件内容是简单的文本的话,从163是可以发到QQ上的;当邮件内容是复杂的MIME邮件时,从163上发不到QQ上,但从QQ上是可以发到163上的,您知道这是怎么回事吗
我建议你最好自己实现一个最小的SMPT语法集合,使用自己的程序
QQ邮箱 POP3 和 SMTP 服务器地址设置如下:邮箱 POP3服务器(端口110) SMTP服务器(端口25)
qq.com pop.qq.com smtp.qq.com
SMTP服务器需要身份验证。
如果是设置POP3和SMTP的SSL加密方式,则端口如下:
POP3服务器(端口995)
SMTP服务器(端口465或587)。不知道你的端口是否设置正确。如果不是端口的问题,那么也可能是这两个邮箱传输期间有问题(我记得好像是163不支持对qq的pop3传输)。