📄 sendmail.java
字号:
package com.suninformation.tools;
import javax.mail.*;
import javax.mail.internet.*;
//import javax.activation.*;
import java.util.*;
import com.suninformation.tools.SunGlobal;
import com.suninformation.user.UnacceptableException;
public class SendMail {
private String mHost = null;
private String mPort = null;
private String mDebug = null;
public void Initialize() throws UnacceptableException {
try {
mHost = SunGlobal.getPersonValue("mail.host");
mPort = SunGlobal.getPersonValue("mail.port");
mDebug = SunGlobal.getPersonValue("mail.debug");
} catch (UnloadPropertiesException e) {
throw new UnacceptableException("发送邮件类初始化失败。", e);
}
}
public void send(String from, String to, String subject, String content)
throws UnacceptableException {
try {
Properties props = new Properties();
Session sendMailSession;
props.put("mail.smtp.host", mHost);
sendMailSession = Session.getInstance(props, null);
//信息对象反映发送的邮件
Message newMessage = new MimeMessage(sendMailSession);
//获取e-mail发送页面的信息
newMessage.setFrom(new InternetAddress(from));
newMessage.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
newMessage.setSubject(subject);
newMessage.setSentDate(new Date());
newMessage.setText(content);
//transport = sendMailSession.getTransport("smtp");
Transport.send(newMessage);
} catch (Exception m) {
throw new UnacceptableException("发送邮件失败。" + m.getMessage(), m);
}
}
public void send(String smtpServer, String from, String to, String subject,
String body) {
try {
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
to, false));
// -- We could include CC recipients too --
// if (cc != null)
// msg.setRecipients(Message.RecipientType.CC
// ,InternetAddress.parse(cc, false));
// -- Set the subject and body text --
msg.setSubject(subject);
msg.setText(body);
// -- Set some other header information --
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());
// -- Send the message --
Transport.send(msg);
System.out.println("Message sent OK.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -