2015-01-09 2 views
2

모든 유형의 호스트와 함께 보내야하는 메일 프로그램이 필요합니다. smtp를 사용했지만 gmail 만 지원합니다. 프로젝트에서 메일은 회사 login.There에서 보내야합니다. 회사 로그인의 그들은 서로 다른 유형의 메일 계정을 가질 수 있습니다 ... 나는 모든 유형의 메일 계정을 지원하는 단일 프로그램을 작성해야합니다.서블릿을 사용하여 JSP로 메일을 보내려면

package com.admin; 

/** 
* 
* @author asp.net 
*/ 
import java.util.Date; 
import java.util.Properties; 

import javax.mail.Authenticator; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
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; 

/** 
* A utility class for sending e-mail messages 
* @author www.codejava.net 
* 
*/ 
public class EmailUtility { 
    public static void sendEmail(String host, String port, 
      final String userName, final String password, String toAddress, 
      String subject, String message) throws AddressException, 
      MessagingException { 

     // sets SMTP server properties 
     Properties properties = new Properties(); 
     properties.put("mail.smtp.host", host); 
     properties.put("mail.smtp.port", port); 
     properties.put("mail.smtp.auth", "true"); 
     properties.put("mail.smtp.starttls.enable", "true"); 
     properties.put("mail.transport.protocol", "smtp") ; 

     // creates a new session with an authenticator 
     Authenticator auth = new Authenticator() { 
      public PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(userName, password); 
      } 
     }; 

     Session session = Session.getInstance(properties, auth); 

     // creates a new e-mail message 
     Message msg = new MimeMessage(session); 

     msg.setFrom(new InternetAddress(userName)); 
     InternetAddress[] toAddresses = { new InternetAddress(toAddress) }; 
     msg.setRecipients(Message.RecipientType.TO, toAddresses); 
     msg.setSubject(subject); 
     msg.setSentDate(new Date()); 
     msg.setText(message); 

     // sends the e-mail 
     Transport.send(msg); 

    } 
} 
+3

고용주가 여러분에게 제공 한 도전적이고 독특한 기회가 무엇인지, 저는 여러분에게 행운이 있기를 바란다 고 생각합니다. –

+0

여러 가지 유형의 메일 계정에 대해 SMTP를 사용하려고하면 어떤 오류가 발생 했습니까? 코드 공유 –

+1

감사합니다 - Elliott Frisch – Priya

답변

0

는 .I이를 CentOS에서 테스트 한 아래의 워킹되는 코드는 6

import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Properties; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 

@WebServlet(name = "SendMail", urlPatterns = {"/SendMail"}) 
public class SendMail extends HttpServlet { 

    public final String from = "[email protected]"; 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 
      // Recipient's email ID needs to be mentioned. 
     String to = "[email protected]"; 
    // Sender's email ID needs to be mentioned 


     // Assuming you are sending email from localhost 
     String host = "smtp.gmail.com"; 

     // Get system properties 
     Properties properties = System.getProperties(); 

     // Setup mail server 
    // properties.setProperty("mail.smtp.host", host); 


       properties.put("mail.smtp.auth", "true"); 
     properties.put("mail.smtp.starttls.enable", "true"); 
     properties.put("mail.smtp.host", "smtp.gmail.com"); 

     properties.put("mail.smtp.port", "587"); 

     // Get the default Session object. 
       Session session ; 

       session = Session.getInstance(properties, 
       new javax.mail.Authenticator() { 

        protected PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication(from, "password"); 
        } 
       }); 

     session.setDebug(true); 


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

     // Set From: header field of the header. 
     message.setFrom(new InternetAddress(from)); 

     // Set To: header field of the header. 
     message.addRecipient(Message.RecipientType.TO, 
            new InternetAddress(to)); 

     // Set Subject: header field 
     message.setSubject("This is the Subject Line!"); 

     // Now set the actual message 
     message.setText("This is actual message"); 

     // Send message 
     Transport.send(message); 
     System.out.println("Sent message successfully...."); 
     }catch (MessagingException mex) { 
     mex.printStackTrace(); 
     } 
    } 



} 

이 서블릿 그리고 당신은 클래스에이 코드를 변경하고 원하는 경우의 기능에 fileds를 통과하지 못할 그 계급.

관련 문제