/*
* EMailSender.java
*
*/
package com.projectName.mail;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.Security;
import java.sql.SQLException;
import java.util.Properties;
public class EMailSender {
private final static String SMTP_HOST_NAME = "smtp_host_name";
private final static String SMTP_AUTH = "smtp_auth";
private final static String DEBUG = "debug";
private final static String SMTP_PORT = "smtp_port";
private final static String SMTP_SOCKETFACTORY_PORT = "smtp_socketfactory_port";
private final static String SMTP_SOCKETFACTORY_CLASS = "smtp_socketfactory_class";
private final static String SMTP_SOCKETFACTORY_FALLBACK = "smtp_socketfactory_fallback";
private final static String EMAIL_SENDER = "email_sender";
private final static String EMAIL_SENDER_PASSWORD = "email_sender_password";
private Properties _emailproperties = null;
/** Creates a new instance of GoogleMailSender */
public EMailSender(Properties emailProperties) {
_emailproperties = emailProperties;
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
}
/**
* Sends email using SSL protocol.
*
* @param recipient
* The mail address of the recipient. Recipient is the person who
* will receive the mail.
* @param subject
* The subject of the mail.
* @param message
* The content of the message.
* @throws MessagingException
* Problem during sending message.
*/
public void sendSSLMessageAuthenticated(String recipient, String subject, String message)
throws MessagingException {
Properties props = new Properties();
props.put("mail.smtp.host", _emailproperties
.getProperty(SMTP_HOST_NAME));
props.put("mail.smtp.auth", _emailproperties.getProperty(SMTP_AUTH,
"true"));
props.put("mail.debug", _emailproperties.getProperty(DEBUG, "false"));
props.put("mail.smtp.port", _emailproperties.getProperty(SMTP_PORT));
props.put("mail.smtp.socketFactory.port", _emailproperties
.getProperty(SMTP_SOCKETFACTORY_PORT));
props.put("mail.smtp.socketFactory.class", _emailproperties
.getProperty(SMTP_SOCKETFACTORY_CLASS));
props.put("mail.smtp.socketFactory.fallback", _emailproperties
.getProperty(SMTP_SOCKETFACTORY_FALLBACK, "false"));
// props.setProperty("proxySet", "true");
// props.setProperty("http.proxyHost", "111.111.111.111");
// props.setProperty("http.proxyPort", "81");
final String sender = _emailproperties.getProperty(EMAIL_SENDER);
final String password = _emailproperties.getProperty(
EMAIL_SENDER_PASSWORD, "");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(sender, password);
}
});
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(sender);
msg.setFrom(addressFrom);
InternetAddress addressTo = new InternetAddress(recipient);
msg.setRecipient(Message.RecipientType.TO, addressTo);
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
/**
* Sends email using SSL protocol.
*
* @param recipient
* The mail address of the recipient. Recipient is the person who
* will receive the mail.
* @param subject
* The subject of the mail.
* @param message
* The content of the message.
* @throws MessagingException
* Problem during sending message.
*/
public void sendMessageUnauthenticated(String recipient, String subject,
String message) throws MessagingException {
Properties props = new Properties();
props.put("mail.smtp.host", _emailproperties
.getProperty(SMTP_HOST_NAME));
props.put("mail.smtp.auth", _emailproperties.getProperty(SMTP_AUTH,
"false"));
props.put("mail.debug", _emailproperties.getProperty(DEBUG, "false"));
props.put("mail.smtp.port", _emailproperties.getProperty(SMTP_PORT));
final String sender = _emailproperties.getProperty(EMAIL_SENDER);
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(sender);
msg.setFrom(addressFrom);
InternetAddress addressTo = new InternetAddress(recipient);
msg.setRecipient(Message.RecipientType.TO, addressTo);
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
/**
* test the function(including {@codeutil.portal} in {@codeutil.test} caused
* some problems so test is done via the main method)
*
* @param args
* @throws IOException
* @throws MessagingException
* @throws SQLException
*/
public static void main(final String args[]) throws IOException,
MessagingException {
EMailSender _emailSender = null;
Properties portalProperties = new Properties();
FileInputStream fis = new FileInputStream(
"C:\\project_home\\mail.properties");
portalProperties.load(fis);
fis.close();
_emailSender = new EMailSender(portalProperties);
_emailSender.sendMessageUnauthenticated("[email protected]",
"Test!", "Send by TestEmailSender!");
}
}
填寫屬性文件或使其硬編碼。
您需要從IT人員獲得SMTP信息(主機/端口)爲exxxxtechnology.com(貴公司),並需要在你的代碼放在那裏。它會和gmail一樣工作。 – Ketan
你在問如何使用Java郵件API發送郵件,或您有任何其他的概率? –
我問,如何通過採取電子郵件作爲輸入 –