2012-03-05 68 views
2

下面是我的代碼,它不會給我提供任何錯誤,但是我的帳戶中也沒有收到電子郵件。我瀏覽了所有關於此事的帖子,並相應地修改了我的代碼。我對這件事情並不陌生,所以這個問題似乎很愚蠢,但仍然歡迎任何方向/建議。此外,該servlet將在谷歌應用程序引擎上運行。我使用我的Gmail帳戶用戶名和密碼而不是[email protected]和密碼。謝謝。使用JavaMail API通過servlet發送的電子郵件沒有通過

import java.io.IOException; 
import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.NoSuchProviderException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
public class UserFeedback extends HttpServlet 
{ 
public void doPost(HttpServletRequest req, HttpServletResponse res) 
{ 
    sendFeedback(req, res); 
} 

private void sendFeedback(HttpServletRequest req, HttpServletResponse res) 
{ 
    String from = null, sub = null, msg = null; 
    String host = "smtp.gmail.com", username = "[email protected]", password = "password"; 
    Session session = null; 
    MimeMessage email = null; 
    Transport transport = null; 

    sub = req.getParameter("subject"); 
    from = req.getParameter("sender"); 
    msg = req.getParameter("message"); 
    msg = "From: " + from + "\n" + msg; 

    Properties props = System.getProperties(); 
    props.setProperty("mail.transport.protocol", "smtp"); 
    props.setProperty("mail.host", host); 
    props.put("mail.smtp.user", username); 
    props.put("mail.smtp.password", password); 
    props.put("mail.smtp.port", "465"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.debug", "true"); 
    props.put("mail.smtp.socketFactory.port", "465"); 
    props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.socketFactory.fallback", "false"); 

    session = Session.getDefaultInstance(props, new javax.mail.Authenticator(){ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("[email protected]", "password");}}); 
    session.setDebug(true); 

    email = new MimeMessage(session); 
    try 
    { 
     email.setSender(new InternetAddress(username)); 
     email.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]")); 
     email.setSubject(sub); 
     email.setContent(msg, "text/plain"); 
    } 
    catch (AddressException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    catch (MessagingException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try 
    { 
     Transport.send(email); 
    } 
    catch (NoSuchProviderException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    catch (MessagingException e) 
    { 
     e.printStackTrace(); 
    } 
} 
} 
+0

你檢查過垃圾文件夾嗎? – 2012-03-05 08:46:06

+0

是的,我沒有檢查我的垃圾郵件/垃圾文件夾;郵件也不在那裏。 – 2012-03-05 08:47:28

+0

哦,是的,有一件事是我試圖將郵件發送到同一個帳戶,因爲我正在測試它,但我不認爲這會導致任何問題。雖然我不確定。 – 2012-03-05 09:07:43

回答

0

您應該使用JavaMail的許多Facade實現之一。將大大清理您的代碼。

要了解此代碼的工作,編寫單元測試,並使用兩種:

這將讓你快速得到答案

0

你是使用:

Properties props = System.getProperties(); 

使用這個代替:

Properties props = new Properties(); 

如果它不能幫助發佈服務器日誌這表明電子郵件被髮送已經。

0

這是一種類型的發送郵件應用..... 而且其靜態應用程序,它的工作的,我們制定了從這個動態應用only..so試一次.. 添加這個jar文件 DSN。罐子,imap.jar,mailapi.jar和pop3.jar,smtp.jar。

package Javamail; 
/* 
Some SMTP servers require a username and password authentication before you 
can use their Server for Sending mail. This is most common with couple 
of ISP's who provide SMTP Address to Send Mail. 

This Program gives any example on how to do SMTP Authentication 
(User and Password verification) 

This is a free source code and is provided as it is without any warranties and 
it can be used in any your code for free. 

*/ 

import javax.mail.*; 
import javax.mail.internet.*; 
import java.util.*; 
import java.io.*; 

/* 
    To use this program, change values for the following three constants, 
    SMTP_HOST_NAME -- Has your SMTP Host Name 
    SMTP_AUTH_USER -- Has your SMTP Authentication UserName 
    SMTP_AUTH_PWD -- Has your SMTP Authentication Password 
    Next change values for fields 
    emailMsgTxt -- Message Text for the Email 
    emailSubjectTxt -- Subject for email 
    emailFromAddress -- Email Address whose name will appears as "from" address 
    Next change value for "emailList". 
    This String array has List of all Email Addresses to Email Email needs to be sent to. 
    Next to run the program, execute it as follows, 
    SendMailUsingAuthentication authProg = new SendMailUsingAuthentication(); 
*/ 

public class SendmailUsejavamail 
{ 

    private static final String SMTP_HOST_NAME = "smtp.gmail.com"; 
    private static final String SMTP_AUTH_USER = "[email protected]"; 
    private static final String SMTP_AUTH_PWD = "xxxxxxxxx"; 

    private static final String emailMsgTxt  = "Online Order Confirmation Message. Also include the Tracking Number."; 
    private static final String emailSubjectTxt = "Order Confirmation Subject"; 
    private static final String emailFromAddress = "[email protected]"; 

    // Add List of Email address to who email needs to be sent to 
    private static final String[] emailList = {"[email protected]", "[email protected]"}; 

    public static void main(String args[]) throws Exception 
    { 
    SendmailUsejavamail smtpMailSender = new SendmailUsejavamail(); 
    smtpMailSender.postMail(emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress); 
    System.out.println("Sucessfully Sent mail to All Users"); 
    } 

    public void postMail(String recipients[ ], String subject, 
          String message , String from) throws MessagingException 
    { 
    boolean debug = false; 

    //Set the host smtp address 
    Properties props = new Properties(); 
    props.put("mail.smtp.host", SMTP_HOST_NAME); 
    props.put("mail.smtp.auth", "true"); 
    Authenticator auth = new SMTPAuthenticator(); 
    Session session = Session.getDefaultInstance(props, auth); 
    session.setDebug(debug); 

    // create a message 
    Message msg = new MimeMessage(session); 

    // set the from and to address 
    InternetAddress addressFrom = new InternetAddress(from); 
    msg.setFrom(addressFrom); 
    InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
    for (int i = 0; i < recipients.length; i++) 
    { 
     addressTo[i] = new InternetAddress(recipients[i]); 
    } 
    msg.setRecipients(Message.RecipientType.TO, addressTo); 
    // Setting the Subject and Content Type 
    msg.setSubject(subject); 
    msg.setContent(message, "text/plain"); 
    Transport.send(msg); 
} 
/** 
* SimpleAuthenticator is used to do simple authentication 
* when the SMTP server requires it. 
*/ 
private class SMTPAuthenticator extends javax.mail.Authenticator 
{ 

    public PasswordAuthentication getPasswordAuthentication() 
    { 
     String username = SMTP_AUTH_USER; 
     String password = SMTP_AUTH_PWD; 
     return new PasswordAuthentication(username, password); 
    } 
} 

}