当前位置: 技术问答>java相关
一个简单的问题,高手帮帮忙,在线等待!
来源: 互联网 发布时间:2015-07-09
本文导语: 一个发送邮件的简单程序! package webmail; /** * Title: * Description: * Copyright: Copyright (c) 2002 * Company: * @author unascribed * @version 1.0 */ import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; import...
一个发送邮件的简单程序!
package webmail;
/**
*
*
*
*
* @author unascribed
* @version 1.0
*/
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
public class mymail {
public mymail() {
}
public static void main(String[] args){
try{
Properties props=new Properties();
props.put("mail.host","163.net");
Session mailConnection=Session.getInstance(props,null);
Message msg=new MimeMessage(mailConnection);
Address bill=new InternetAddress("god@micorosoft.com");
Address elliotte=new InternetAddress("zmrljl@yeah.net");
msg.setContent("resistansce is futile ,tyou wiil be assistant!","text/plain");
msg.setFrom(bill);
msg.setRecipient(Message.RecipientType.TO,elliotte);
msg.setSubject("you must ok");
Transport.send(msg);
}
catch(Exception e){
e.printStackTrace() ;
}
}
}
总是提示如下信息
avax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class javax.mail.SendFailedException: 550 : Invalid User
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at webmail.mymail.main(mymail.java:31)
高手解释一下为何啊!
package webmail;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2002
*
Company:
* @author unascribed
* @version 1.0
*/
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
public class mymail {
public mymail() {
}
public static void main(String[] args){
try{
Properties props=new Properties();
props.put("mail.host","163.net");
Session mailConnection=Session.getInstance(props,null);
Message msg=new MimeMessage(mailConnection);
Address bill=new InternetAddress("god@micorosoft.com");
Address elliotte=new InternetAddress("zmrljl@yeah.net");
msg.setContent("resistansce is futile ,tyou wiil be assistant!","text/plain");
msg.setFrom(bill);
msg.setRecipient(Message.RecipientType.TO,elliotte);
msg.setSubject("you must ok");
Transport.send(msg);
}
catch(Exception e){
e.printStackTrace() ;
}
}
}
总是提示如下信息
avax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class javax.mail.SendFailedException: 550 : Invalid User
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at webmail.mymail.main(mymail.java:31)
高手解释一下为何啊!
|
163.net需要smtp认证,你要自己继承一个 Authenticator 类,比如:
public class MailAuth extends Authenticator
{
String userName,password;
public MailAuth(String user,String pass)
{
super();
userName=new String(user);
password=new String(pass);
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName,password);
}
}
然后:
Properties props = new Properties();
//初始化参数
props.put("mail.smtp.host",smtpHost);
props.put("mail.transport.protocol","smtp");
props.put("mail.transport.port",smtpPort);
props.put("mail.smtp.auth","true"); //这行一定要有
//创建Session
MailAuth ma=new MailAuth(smtpUser,smtpPass);
Session mailSession=Session.getInstance(props,ma);
这样就行了
public class MailAuth extends Authenticator
{
String userName,password;
public MailAuth(String user,String pass)
{
super();
userName=new String(user);
password=new String(pass);
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName,password);
}
}
然后:
Properties props = new Properties();
//初始化参数
props.put("mail.smtp.host",smtpHost);
props.put("mail.transport.protocol","smtp");
props.put("mail.transport.port",smtpPort);
props.put("mail.smtp.auth","true"); //这行一定要有
//创建Session
MailAuth ma=new MailAuth(smtpUser,smtpPass);
Session mailSession=Session.getInstance(props,ma);
这样就行了