2012-03-05 48 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); 
    } 
} 

}