簡體   English   中英

java.net.MalformedURLException smtp協議

[英]java.net.MalformedURLException smtp protocol

我正在使用以下代碼,以便在我的java應用程序中使用smtp。

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

使用它時顯示錯誤

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

即使我嘗試使用SMTP代替com.sun.mail.smtp但沒有用..什么是用於smtp的協議名稱?

如果您嘗試通過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 = "youremailID@gmail.com";

   /**
     * @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("sender@email.com", "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 = "youremail@gmail.com";
  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);
  }
}

希望可能有所幫助。

問候

smtp不是受支持的協議(至少從1.5開始),您將使用mailto協議。 請參閱以下示例禮貌的谷歌....

http://www.java2s.com/Code/Java/Network-Protocol/sendsemailusingamailtoURL.htm

注意防火牆和您選擇的主機端口,然后嘗試使用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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM