java.util.Properties props=System.getProperties();
props.put("mail.host",mailserver);
props.put("mail.transport.protocol","smtp");
props.put("mail.transport.user","buyc");//这里是加入用户名
props.put("mail.transport.password","buyc");//这里是加入密码
props.put("mail.host",mailserver);
props.put("mail.transport.protocol","smtp");
props.put("mail.transport.user","buyc");//这里是加入用户名
props.put("mail.transport.password","buyc");//这里是加入密码
现在一般的服务器大多是需要身份验证的呀(smtp协议)!
再发 email前,要了解你所发向的服务器支持那种的协议!
Session sendMailSession;
Store store;
Transport transport;
sendMailSession = Session.getInstance(props, null);
props.put("mail.smtp.host","idc71.com");
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress( "[email protected]"));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress (email));
newMessage.setSubject(new String(mailsubject.getBytes("gb2312")));
newMessage.setSentDate(now);
newMessage.setText(new String(mess.getBytes("gb2312")));
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
不过用户名和密码指的是收邮件者的用户名和密码,不是发送邮件的人的.
import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;/**
* 带身份验证的 email 发送程序
*
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2001</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class Email3 { public static void main(String[] argv) {
new Email3();
} public Email3() {
//收件人
String to = null;
//发件人
String from = null;
//主题
String subject = null;
// mail 主机
String mailhost = null;
// mail 内容
String content = null;
//MIME邮件对象
MimeMessage mimeMsg = null;
//邮件会话对象
Session session = null;
//************ 不同之处 *************/
String user = null;
String password = null;
try {
mailhost = "smtp.sina.com.cn";
from = "[email protected]";
to = "[email protected]";
subject = "您好";
content = "带身份验证的";
user = "nora";
password = "00000"; Properties props = System.getProperties(); //获得系统属性
props.put("mail.smtp.host", mailhost); //设置SMTP主机
props.put("mail.smtp.auth","true"); //设置身份验证为真,若须身份验证则必须设为真
// session = Session.getDefaultInstance(props,null);
//注意下面这行的 Session.getDefaultInstance 方法的第二个参数
session = Session.getDefaultInstance(props, new Email_Autherticatorbean(user, password)); //创建MIME邮件对象
mimeMsg = new MimeMessage( session );
//设置发信人
mimeMsg.setFrom(new InternetAddress( from ) ); //设置收信人
if(to!=null){
mimeMsg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( to ) );
} //设置邮件主题
mimeMsg.setSubject(subject,"GBK"); //设置邮件内容
mimeMsg.setText(content ,"GBK" );
//发送日期
mimeMsg.setSentDate(new Date());
//发送邮件
Transport.send( mimeMsg );
System.out.println( "email send!"); } catch (Exception e) {
e.printStackTrace();
}
}
}class Email_Autherticatorbean extends Authenticator
{
private String username;
private String password;
public Email_Autherticatorbean(String u, String p)
{
username = u;
password = p;
}
//一定要有这个方法,它是在需要身份验证时自动被调用的
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
}
/**
* 带身份验证的邮件要用到的一个类</p>
* 注意一定要继承 Authenticator 类,并覆盖 getPasswordAuthentication 方法
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2001</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
import javax.mail.*; public class Email_Autherticatorbean extends javax.mail.Authenticator
{
private String m_username = null;
private String m_userpass = null; public void setUsername(String username)
{
m_username = username;
} public void setUserpass(String userpass)
{
m_userpass = userpass;
} public Email_Autherticatorbean()
{
super();
}
public Email_Autherticatorbean(String username, String userpass)
{
super();
setUsername(username);
setUserpass(userpass);
}
//一定要有这个方法,它是在需要身份验证时自动被调用的
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(m_username,m_userpass);
}
}