javamail的问题

来源:百度知道 编辑:UC知道 时间:2024/09/28 10:31:08
package com.hbcf.bean;
public class TestMail {
private String host;
private String username;
private String password;
private String from;
private String to;
private String subject;
private String content;
public TestMail()
{}
//set get 方法我省略了 太长发不了
//======发送邮件的方法==========
public boolean sendmail(){
//存储连接参数
Properties props=new Properties();
props.put("host", this.host);
props.put("auth", "true");
MyAuthenticator as=new MyAuthenticator(username,password);
Session session=Session.getDefaultInstance(props, as);
Message msg=new MimeMessage(session);
try {
// 设置源地址,这里的setFrom方法是Message对象设置发送地址方法的
msg.setFrom(new InternetAddress(from));
// 创建邮件的接收者地址,并设置到邮件消息中
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
//格式(MimeBodyPart类型的对象)-->内容(MultiPart类型的对象)-->消息(Message类型的对象)

好像是你这个发件箱没有开启smtp功能的原因吧,我以前用163作为发件箱的时候也报错,也不知道163的邮箱在哪里设置开启邮箱smtp设置。后来就用sina的邮箱作为发件箱,那里面有指定在哪儿可以开启pop3/smtp设置