无认证方式发送
///限于无须认证的邮件服务器,
String host = "smtp.sss.com"; //邮件服务器
String from = " xxxxx@163.com"; //发送人
String to = " xxx@163.com"; //收信人
String host = "smtp.sss.com"; //邮件服务器
String from = " xxxxx@163.com"; //发送人
String to = " xxx@163.com"; //收信人
// 得到系统属性
Properties props = System.getProperties();
// 设置发送服务器
props.put("mail.smtp.host", host);
Properties props = System.getProperties();
// 设置发送服务器
props.put("mail.smtp.host", host);
// 建立session
Session session = Session.getDefaultInstance(props, null);
// 定义message
MimeMessage message = new MimeMessage(session);
// 定义message
MimeMessage message = new MimeMessage(session);
//设置收信人,多个收信人也可以用数组表示
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("subject");//设置显示的主题
message.setFrom(new InternetAddress(from));//设置显示的发送人
message.setText("text field");//设置邮件内容
message.setSentDate(new Date());//设置收送日期
message.setFrom(new InternetAddress(from));//设置显示的发送人
message.setText("text field");//设置邮件内容
message.setSentDate(new Date());//设置收送日期
// 发送
Transport.send(message);
Transport.send(message);
认证方式发信
认证类
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.PasswordAuthentication;
public class MyPasswordAuthentication extends Authenticator{
public MyPasswordAuthentication() {
// TODO Auto-generated constructor stub
}
// TODO Auto-generated constructor stub
}
String username = null;
String password = null;
PasswordAuthentication pa = null;
public static MyPasswordAuthentication instance = null;
public static synchronized MyPasswordAuthentication getInstance(String user,String pass)
{
if(instance == null)
{
instance = new MyPasswordAuthentication(user,pass);
}
return instance;
}
public MyPasswordAuthentication(String user,String pass)
{
username = user;
password = pass;
}
public PasswordAuthentication getPasswordAuthentication()
{
if(pa == null)
{
pa = new PasswordAuthentication(username, password);
}
return pa;
}
}
{
username = user;
password = pass;
}
public PasswordAuthentication getPasswordAuthentication()
{
if(pa == null)
{
pa = new PasswordAuthentication(username, password);
}
return pa;
}
}
//发送类
import java.util.Date;
import java.util.Properties;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.smtp.SMTPTransport;
public class SendMail{
public SendMail()
{
try {
send();
} catch (MessagingException e) {
e.printStackTrace();
}
}
{
try {
send();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public void send() throws MessagingException
{
String host = "mail5.ujs.edu.cn"; //邮件服务器
String name = "xxx"; //用户名
String password ="xxxxx"; //密码
String from = " xxx@163.com"; //发送人
String to = " xxxxx@163.com"; //收信人
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
{
String host = "mail5.ujs.edu.cn"; //邮件服务器
String name = "xxx"; //用户名
String password ="xxxxx"; //密码
String from = " xxx@163.com"; //发送人
String to = " xxxxx@163.com"; //收信人
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");
Authenticator auth = new MyPasswordAuthentication().getInstance(name,password);
Authenticator auth = new MyPasswordAuthentication().getInstance(name,password);
Session session = Session.getDefaultInstance(props, auth);// 创建session
MimeMessage message = new MimeMessage(session); //创建message对象
MimeMessage message = new MimeMessage(session); //创建message对象
Address address = new InternetAddress(from); //发件人地址
Address toAddress = new InternetAddress(to);
Address[] toAddList = new Address[5];
toAddList[0] = new InternetAddress(" xxx1@163.com");
toAddList[1] = new InternetAddress(" ddsds@163.com");
toAddList[2] = new InternetAddress(" sadsf@163.com");
toAddList[3] = new InternetAddress(" asdf@163.com");
toAddList[4] = new InternetAddress(" asdf@163.com");
message.setFrom(address); //设置发件人
message.setRecipients(MimeMessage.RecipientType.TO, toAddList);
message.setSubject("hahahahahaha", "GB2312"); //设置主题
message.setSentDate(new Date()); //设置日期
message.setText("hahahahaha", "GB2312"); //设置邮件内容
message.setFrom(address);
Address[] toAddList = new Address[5];
toAddList[0] = new InternetAddress(" xxx1@163.com");
toAddList[1] = new InternetAddress(" ddsds@163.com");
toAddList[2] = new InternetAddress(" sadsf@163.com");
toAddList[3] = new InternetAddress(" asdf@163.com");
toAddList[4] = new InternetAddress(" asdf@163.com");
message.setFrom(address); //设置发件人
message.setRecipients(MimeMessage.RecipientType.TO, toAddList);
message.setSubject("hahahahahaha", "GB2312"); //设置主题
message.setSentDate(new Date()); //设置日期
message.setText("hahahahaha", "GB2312"); //设置邮件内容
message.setFrom(address);
//发送
// Transport transport = session.getTransport("smtp");
// Transport.send(message,toAddList);
// Transport.send(message,toAddList);
SMTPTransport t = (SMTPTransport)session.getTransport("smtp");
t.connect(host,name,password);
t.sendMessage(message,message.getAllRecipients());
t.connect(host,name,password);
t.sendMessage(message,message.getAllRecipients());
}
public static void main(String[] args)
{
new SendMail();
}
public static void main(String[] args)
{
new SendMail();
}
}