2013-07-04 65 views
1

我想使代碼可以通過usibg的JavaMail API發送恢復密碼,從ABC一樣都是我的官方域名地址發送發送郵件恢復到任何用戶的電子郵件@ xyz.com。如何從我們的域名地址([email protected])使用JavaMail API

我去一些編碼像使用smtp.gmail.com

BLOG我提到將郵件發送到任何用戶的電子郵件地址。

this structure i want: 

from : [email protected](not [email protected] ) 
to: [email protected], [email protected]/[email protected] /[email protected] 

is it possible ? 
+0

您需要從IT人員獲得SMTP信息(主機/端口)爲exxxxtechnology.com(貴公司),並需要在你的代碼放在那裏。它會和gmail一樣工作。 – Ketan

+0

你在問如何使用Java郵件API發送郵件,或您有任何其他的概率? –

+0

我問,如何通過採取電子郵件作爲輸入 –

回答

0

爲此,您需要服務器安全證書爲您的smtp服務器,您需要購買或獲取某處。

+0

K,感謝這個信息 –

+0

請解釋爲什麼需要CA頒發的服務器證書將郵件發送到特定域的地址? –

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("[email protected]", 
      "Test!", "Send by TestEmailSender!"); 
} 

}

填寫屬性文件或使其硬編碼。

+0

k,謝謝。我會嘗試這個代碼。 –

+0

注意,上面的碼充滿[共同JavaMail的錯誤(http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes)。 –

相關問題