2012-01-08 35 views
1

我正在使用以下代碼inorder在我的Java應用程序中使用smtp。java.net.MalformedURLEception smtp協議

URL url=new URL("com.sun.mail.smtp","smtp.gmail.com",25,""); 

在使用該它顯示的錯誤

java.net.MalformedURLException: unknwon protocol: com.sun.mail.smtp 

即使我試圖在地方com.sun.mail.smtp,但沒有用使用SMTP。什麼是要使用的協議名稱SMTP?

+0

你想發送電子郵件嗎? – 2012-01-08 06:28:25

+0

是的..我試圖發送電子郵件 – user636207 2012-01-08 06:50:36

+0

我不會直接嘗試這樣做,而是使用可用的第三方庫之一,例如, [commons-email](http://commons.apache.org/email/)與[java郵件API](http://www.oracle.com/technetwork/java/javamail/index.html)結合使用。 – centic 2012-01-08 08:27:23

回答

2

如果您正試圖通過javax.mail API發送郵件,你可以使用這個

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.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class SendMailToMyself 
{ 
    private static final String SMTP_HOST_NAME = "smtp.gmail.com"; 
    private static final String MY_EMAIL = "[email protected]"; 

    /** 
    * @param emailContact : Email of the person who contact you or From whom the email came. 
    * @param subject : Subject of the Email send by the contact. 
    * @param msgFromContact : Message Text of the Body. 
    * 
    * The method is used to take EmailID of the Contact, Subject of the Message, 
    * Message Text as the input, as provided on the JSP side Contact Me page and 
    * sends it to the Administrator of the Website on his Mail Address. 
    */ 

    public void postMail(String emailContact, String subject, String msgFromContact) 
                 throws MessagingException 
    { 
    boolean debug = false; 

    // Set the host smtp address 
    Properties prop = new Properties(); 
    prop.put("mail.smtp.host", SMTP_HOST_NAME);   
    /* 
     * Do remember to remove the below line from comment, if your mail server does support TLS (port 587), SSL(port 465) security features. 
     * Like if you sending a mail to Hotmail or gmail this must be uncommented, and then you have to use above ports 
     * instead of port 25. 
     */ 
    prop.put("mail.smtp.starttls.enable", "true"); 
    prop.put("mail.smtp.port", "587"); 
    prop.put("mail.smtp.auth", "true"); 

    Authenticator auth = new SMTPAuthenticator(); 
    Session session = Session.getDefaultInstance(prop, auth); 

    session.setDebug(debug); 

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

    // Set the from and to address. 
    InternetAddress addressFrom = new InternetAddress(emailContact); 
    msg.setFrom(addressFrom); 

    InternetAddress addressTo = new InternetAddress(MY_EMAIL); 

    msg.setRecipient(Message.RecipientType.TO, addressTo); 

    // Setting the subject and content Type 
    msg.setSubject(subject); 
    msg.setContent(msgFromContact, "text/plain"); 
    Transport.send(msg); 
    } 

    public static void main(String... args) throws MessagingException 
    { 
    SendMailToMyself smtm = new SendMailToMyself(); 
    smtm.postMail("[email protected]", "Testing Program", "Hello there, Testing command prompt messaging."); 
    System.out.println("Your Message has been send. Regards"); 
    } 
} 

這裏是SMTPAuthenticator類

import javax.mail.Authenticator; 
import javax.mail.PasswordAuthentication; 

/** 
    * SimpleAuthenticator is used to do simple authentication 
    * when the SMTP server requires it. 
    */ 

public class SMTPAuthenticator extends Authenticator 
{ 
    private static final String SMTP_AUTH_USER = "[email protected]"; 
    private static final String SMTP_AUTH_PASSWORD = "yourpassword"; 

    public PasswordAuthentication getPasswordAuthentication() 
    { 
    String username = SMTP_AUTH_USER; 
    String password = SMTP_AUTH_PASSWORD; 

    return new PasswordAuthentication(username, password); 
    } 
} 

希望這可能會有所幫助。

問候

1

與防火牆和您選擇並嘗試下面的代碼的主機端口保重使用javax.mail API。

import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.mail.*; 
import javax.mail.internet.*; 
import java.util.*; 

final class MailClient 
{ 
    private class SMTPAuthenticator extends Authenticator 
    { 
     private PasswordAuthentication authentication; 

     public SMTPAuthenticator(String login, String password) 
     { 
      authentication = new PasswordAuthentication(login, password); 
     } 

     @Override 
     protected PasswordAuthentication getPasswordAuthentication() 
     { 
      return authentication; 
     } 
    } 

    public void mail() 
    { 
     try 
     { 
      String from = "xyz.com"; 
      String to = "abc.com"; 
      String subject = "Your Subject."; 
      String message = "Message Text."; 
      String login = "xyz.com"; 
      String password = "password"; 

      Properties props = new Properties(); 
      props.setProperty("mail.host", "smtp.gmail.com"); 
      props.setProperty("mail.smtp.port", "587"); 
      props.setProperty("mail.smtp.auth", "true"); 
      props.setProperty("mail.smtp.starttls.enable", "true"); 

      Authenticator auth = new SMTPAuthenticator(login, password); 

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

      MimeMessage msg = new MimeMessage(session); 

      try 
      { 
       msg.setText(message); 
       msg.setSubject(subject); 
       msg.setFrom(new InternetAddress(from)); 
       msg.addRecipient(Message.RecipientType.TO, 
       new InternetAddress(to)); 
       Transport.send(msg); 
      } 
      catch (MessagingException ex) 
      { 
       Logger.getLogger(MailClient.class.getName()). 
       log(Level.SEVERE, null, ex); 
      } 
     } 
    } 
} 

final public class Main 
{ 
    public static void main(String...args) 
    { 
     new MailClient().mail(); 
    } 
}