Java发送邮件
2008-08-01 18:09
399 查看
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dcampus;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*发送邮件测试通过
* @author He Jianfei
*/
public class JavaMail {
private String host;
private String from;
private String to;
private String subject;
private String content;
private boolean authentication;
private String username;
private String password;
public void sendMail(String host, String from, String to, String subject,
String content, boolean authentication, String username,
String password) throws MessagingException {
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
if (!authentication) {
props.put("mail.smtp.auth", "false");
} else {
props.put("mail.smtp.auth", "true");
}
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setContent(content, "text/html;charset=gb2312");
// Send message
if (authentication) {
Transport smtp = null;
try {
smtp = session.getTransport("smtp");
smtp.connect(host, username, password);
smtp.sendMessage(message, message.getAllRecipients());
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} finally {
smtp.close();
}
} else {
Transport.send(message);
}
}
public void send() throws MessagingException {
host = "smtp.scut.edu.cn";
from = "junwang@scut.edu.cn";
to = "wj_526@sina.com";
subject = "just for test";
content = "Beijing Olympic";
authentication = true;
username = "xxxxxxxx";
password = "xxxxxx";
sendMail(host, from, to, subject, content, authentication, username, password);
}
public static void main(String args[]) throws MessagingException {
JavaMail jm = new JavaMail();
jm.send();
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dcampus;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*发送邮件测试通过
* @author He Jianfei
*/
public class JavaMail {
private String host;
private String from;
private String to;
private String subject;
private String content;
private boolean authentication;
private String username;
private String password;
public void sendMail(String host, String from, String to, String subject,
String content, boolean authentication, String username,
String password) throws MessagingException {
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
if (!authentication) {
props.put("mail.smtp.auth", "false");
} else {
props.put("mail.smtp.auth", "true");
}
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setContent(content, "text/html;charset=gb2312");
// Send message
if (authentication) {
Transport smtp = null;
try {
smtp = session.getTransport("smtp");
smtp.connect(host, username, password);
smtp.sendMessage(message, message.getAllRecipients());
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} finally {
smtp.close();
}
} else {
Transport.send(message);
}
}
public void send() throws MessagingException {
host = "smtp.scut.edu.cn";
from = "junwang@scut.edu.cn";
to = "wj_526@sina.com";
subject = "just for test";
content = "Beijing Olympic";
authentication = true;
username = "xxxxxxxx";
password = "xxxxxx";
sendMail(host, from, to, subject, content, authentication, username, password);
}
public static void main(String args[]) throws MessagingException {
JavaMail jm = new JavaMail();
jm.send();
}
}
相关文章推荐
- JAVA邮件发送的简单实现
- 使用JAVA 代码发送邮件
- java程序发送内嵌图片、带附件邮件
- 如何使用Java来调用Notes API发送邮件(包括附件)
- 使用java在本地服务器实现简单的邮件的发送与接受
- java发送简单邮件
- Java发送邮件(spring mail + apache mail)
- java发送Email邮件
- java发送邮件
- java邮件发送
- java发送邮件实现
- JAVA 使用JavaMail发送邮件(mail.jar和activtion.jar)
- Spring JavaMailSenderImpl发送邮件时中文乱码处理
- java 邮件发送
- 基于JavaMail的java邮件发送
- JavaMail技术 用java代码发送邮件
- java 发送邮件(qq邮箱作为发件箱,带附件)
- 用java开发Email工具之发送邮件 (1)作者:冯睿
- java 发送邮件
- Java 构建发送邮件的小应用