📄 sendmail.java
字号:
package cn.bway.common;
/*
* ���������ʼ�����ϵͳ
* Created on 2005-12-15
* Author fhp
* ��������������ʼ�������
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* @author Kson
*
*/
public class SendMail {
// private String EnvelopeFrom;//�ʼ����͵�ַ
// private String MailHost;//�ʼ�������
private String UserName;// �û���
private String PassWord;// �û�����
private MimeMessage mimeMsg; // MIME�ʼ�����
private Session session; // �ʼ��Ự����
private Properties props; // ϵͳ����
// private boolean needAuth = false; //smtp�Ƿ���Ҫ��֤
private Multipart mp; // Multipart����,�ʼ�����,����,���������ݾ���ӵ����к������MimeMessage����
public SendMail() {
}
/**
* public void sendMail() {
* setSmtpHost(getConfig.mailHost);//���û��ָ���ʼ�������,�ʹ�getConfig���л�ȡ
* createMimeMessage(); }
*/
public SendMail(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
}
public void setSmtpHost(String hostName) {
System.out.println("����ϵͳ���ԣ�mail.smtp.host = " + hostName);
if (props == null)
props = System.getProperties(); // ���ϵͳ���Զ���
props.put("mail.smtp.host", hostName); // ����SMTP���
}
public boolean createMimeMessage() {
try {
System.out.println("����ȡ�ʼ��Ự����");
session = Session.getDefaultInstance(props, null); // ����ʼ��Ự����
} catch (Exception e) {
System.err.println("��ȡ�ʼ��Ự����ʱ�������" + e);
return false;
}
System.out.println("������MIME�ʼ�����");
try {
mimeMsg = new MimeMessage(session); // ����MIME�ʼ�����
mp = new MimeMultipart();
return true;
} catch (Exception e) {
System.err.println("����MIME�ʼ�����ʧ�ܣ�" + e);
return false;
}
}
public void setNeedAuth(boolean need) {
System.out.println("����smtp�����֤��mail.smtp.auth = " + need);
if (props == null)
props = System.getProperties();
if (need) {
props.put("mail.smtp.auth", "true");
} else {
props.put("mail.smtp.auth", "false");
}
}
public void setNamePass(String name, String pass) {
UserName = name;
PassWord = pass;
}
public boolean setSubject(String mailSubject) {
System.out.println("�����ʼ����⣡");
try {
mimeMsg.setSubject(mailSubject);
return true;
} catch (Exception e) {
System.err.println("�����ʼ����ⷢ�����");
return false;
}
}
public boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent(
"<meta http-equiv=Content-Type content=text/html; charset=gb2312>"
+ mailBody, "text/html;charset=GB2312");
mp.addBodyPart(bp);
return true;
} catch (Exception e) {
System.err.println("�����ʼ�����ʱ�������" + e);
return false;
}
}
public boolean addFileAffix(String filename) {
System.out.println("����ʼ�������" + filename);
try {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());
mp.addBodyPart(bp);
return true;
} catch (Exception e) {
System.err.println("����ʼ�������" + filename + "�������" + e);
return false;
}
}
public boolean setFrom(String from) {
System.out.println("���÷����ˣ�");
try {
mimeMsg.setFrom(new InternetAddress(from)); // ���÷�����
return true;
} catch (Exception e) {
return false;
}
}
public boolean setTo(String to) {
if (to == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(to));
return true;
} catch (Exception e) {
return false;
}
}
public boolean setCopyTo(String copyto) {
if (copyto == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.CC,
(Address[]) InternetAddress.parse(copyto));
return true;
} catch (Exception e) {
return false;
}
}
public boolean sendout() {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("���ڷ����ʼ�....");
Session mailSession = Session.getInstance(props, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), UserName,
PassWord);
transport.sendMessage(mimeMsg, mimeMsg
.getRecipients(Message.RecipientType.TO));
System.out.println("�����ʼ��ɹ���");
transport.close();
return true;
} catch (Exception e) {
System.err.println("�ʼ�����ʧ�ܣ�" + e);
return false;
}
}
public static void main(String[] args) {
String mailbody = "<meta http-equiv=Content-Type content=text/html; charset=gb2312>"
+ "<div align=center><a href=http://www.csdn.net>����Է�-----------------</a></div>";
SendMail themail = new SendMail("DNMC-MAIL12.gnpjvc.cgnpc.com.cn");
themail.setNeedAuth(true);
if (themail.setSubject("����") == false)
return;
if (themail.setBody(mailbody) == false)
return;
if (themail.setTo("pcityuy@gnpjvc.com.cn") == false)
return;
if (themail.setFrom("pcityuy@gnpjvc.com.cn") == false)
return;
themail.setNamePass("pcityuy", "qqq");
if (themail.sendout() == false)
return;
}
}
/**
*
* @author Administrator
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
class SMTPAuth extends javax.mail.Authenticator {
private String user, password;
public SMTPAuth(String u, String p) {
user = u;
password = p;
}
public void getuserinfo(String getuser, String getpassword) {
user = getuser;
password = getpassword;
}
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(user, password);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -