1

我想制作可以通过 usibg javaMail API 发送恢复密码的代码,以便从我的官方域地址(如 abc@xyz.com)发送。

我进行了一些编码,例如使用 smtp.gmail.com

这个 博客 我提到发送邮件到任何用户的电子邮件地址。

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 ?
4

2 回答 2

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!");
}

}

填充属性文件或使其硬编码。

于 2013-07-04T11:59:06.747 回答
0

为此,您需要 smtp 服务器的服务器安全证书,您需要在某处购买或获取该证书。

于 2013-07-04T11:53:32.863 回答