2013-05-08 115 views
1

我已經編寫了發送郵件的java代碼,當我使用端口號587,並且仍然運行時沒有停止,當我使用端口465時,會發出異常。 我使用Tomcat,而我不知道是否有配置。 Mail.javaJava發送郵件失敗,與SMTP SMTP郵件

public class Mail { 
     String to; 
     String from; 
     String message; 
     String subject; 
     String smtpServ; 
     //getter and setter 
     public int sendMail(){ 
      try 
      { 
       Properties props = System.getProperties(); 
       props.put("mail.transport.protocol", "smtp"); 
       props.put("mail.smtp.starttls.enable","true"); 
       props.put("mail.smtp.host","smtp.gmail.com"); 
       props.put("mail.smtp.auth", "true"); 
       props.put("mail.smtp.port", "456"); 
       Authenticator auth = new SMTPAuthenticator(); 
       Session session = Session.getInstance(props, auth); 
       Message msg = new MimeMessage(session); 
       msg.setFrom(new InternetAddress(from)); 
       msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to,false)); 
       msg.setSubject(subject); 
       msg.setText(message); 
       msg.setHeader("MyMail", "Mr. XYZ"); 
       msg.setSentDate(new Date()); 
       Transport.send(msg); 
       System.out.println("Message sent to"+to+" OK."); 
       return 0; 
      } 
      catch (Exception ex) 
      { 
       ex.printStackTrace(); 
       System.out.println("Exception "+ex); 
       return -1; 
      } 
     } 
    } 


public class SMTPAuthenticator extends javax.mail.Authenticator { 
     @Override 
     public PasswordAuthentication getPasswordAuthentication() { 
      String username = "***@gmail.com";   // specify your email id here (sender's email id) 
      String password = "***";          // specify your password here 
      return new PasswordAuthentication(username, password); 
     } 
    } 

的index.jsp:在

<body> 
<form action="sendMail.jsp" method="POST"> 
<input type="text" name="to" value="" /> 
<input type="text" name="subject" value="" /> 
<textarea name="message" rows="8" cols="30"> 
</textarea> 
<input type="submit" value="Send Mail" /> 

sendMail.jsp

<jsp:useBean id="mail" scope="session" class="mailing.Mail" /> 
<jsp:setProperty name="mail" property="to" param="to" /> 
<jsp:setProperty name="mail" property="from" value="****@gmail.com" /> 
<jsp:setProperty name="mail" property="smtpServ" value="smtp.gmail.com" /> 
<jsp:setProperty name="mail" property="subject" param="subject" /> 
<jsp:setProperty name="mail" property="message" param="message" /> 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
... 
<body> 
<% 
String to = mail.getTo(); 
int result; 
result = mail.sendMail(); 
} 
%> 

回答

2

此代碼正常工作。試試這個

public static void sendEmail(Email mail) { 

String host = "smtp.gmail.com"; 
String from = "YOUR_GMAIL_ID"; 
String pass = "YOUR_GMAIL_PASSWORD"; 
Properties props = System.getProperties(); 
props.put("mail.smtp.starttls.enable", "true"); // added this line 
props.put("mail.smtp.host", host); 
props.put("mail.smtp.user", from); 
props.put("mail.smtp.password", pass); 
props.put("mail.smtp.port", "587"); 
props.put("mail.smtp.auth", "true"); 

// Get the default Session object. 
Session session = Session.getDefaultInstance(props, null); 

try { 
    // Create a default MimeMessage object. 
    MimeMessage message = new MimeMessage(session); 

    // Set sender 
    message.setFrom(new InternetAddress("Senders_EMail_Id")); 

    // Set recipient 
    message.addRecipient(Message.RecipientType.TO, new InternetAddress("RECIPIENT_EMAIL_ID")); 

    // Set Subject: header field 
    message.setSubject("SUBJECT"); 

    // set content and define type 
    message.setContent("CONTENT", "text/html; charset=utf-8"); 

    Transport transport = session.getTransport("smtp"); 
    transport.connect(host, from, pass); 
    transport.sendMessage(message, message.getAllRecipients()); 
    transport.close(); 
    } catch (MessagingException mex) { 
    System.out.println(mex.getLocalizedMessage()); 
} 

}

+0

它給了我錯誤得到歡迎msg – ftning 2013-05-09 16:20:34

+0

它的工作原理,thx :) – ftning 2013-05-09 22:39:49

+0

gr8。古德聽說:) – 2013-05-10 04:59:21

0

在代碼中你有口456。這是行不通的。

+0

是@Michal博雷克我的意思是465,它仍然不停地運行。 – ftning 2013-05-09 16:24:44

0

是端口487將無法正常工作

這裏有一個例子代碼

小號

tring host = "smtp.gmail.com"; 
     String from = "your gmail account"; 
     String pass = "your gmail password"; 
     Properties props = System.getProperties(); 
     props.put("mail.smtp.starttls.enable", "true"); // added this line 
     props.put("mail.smtp.host", host); 
     props.put("mail.smtp.user", from); 
     props.put("mail.smtp.password", pass); 
     props.put("mail.smtp.port", "587"); 
     props.put("mail.smtp.auth", "true"); 

     Session session = Session.getDefaultInstance(props, null); 
     MimeMessage message = new MimeMessage(session); 
     message.setFrom(new InternetAddress(from)); 
     InternetAddress toAddress = new InternetAddress(email); 
     message.addRecipient(Message.RecipientType.TO, toAddress); 

     message.setSubject("Subject of the message"); 
     message.setText("Text Of the Message"); 

     Transport transport = session.getTransport("smtp"); 
     transport.connect(host, from, pass); 
     transport.sendMessage(message, message.getAllRecipients()); 
     transport.close();