如何使用JavaMail API将恢复邮件从我们的域名地址(mine@xxtech.com)发送到任何用户电子邮件

时间:2013-07-04 11:40:16

标签: java javamail

我想创建可以通过usibg javaMail API发送恢复密码的代码,从我的官方域名地址发送,如abc@xyz.com。

我做了一些编码,比如使用smtp.gmail.com

此    BLOG我提到发送邮件到任何用户电子邮件地址。

this structure i want:

 from : deepu@exxxxtechnology.com-----( not deepu@gmail.com  )
 to: anyuser@anydomain.com, user@gmail.com / user@hotmail.com /user@yahoomail.com

 is it possible ?

2 个答案:

答案 0 :(得分:1)

/*
 * 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("test@gmail.com",
            "Test!", "Send by TestEmailSender!");
}

}

填写属性文件或将其硬编码。

答案 1 :(得分:0)

为此,您需要为您的smtp服务器提供服务器安全证书,您需要购买或获取某个服务器安全证书。