简体   繁体   English

java.net.MalformedURLException smtp协议

[英]java.net.MalformedURLException smtp protocol

I am using following code inorder to use smtp in my java application. 我正在使用以下代码,以便在我的java应用程序中使用smtp。

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

While using this it is showing the error 使用它时显示错误

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

Even i tried using SMTP in place of com.sun.mail.smtp but no use.. What is the protocol name to use for smtp? 即使我尝试使用SMTP代替com.sun.mail.smtp但没有用..什么是用于smtp的协议名称?

If you are trying to send mail through javax.mail API you can use this 如果您尝试通过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");
   }
 } 

And here is the SMTPAuthenticator Class 这是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);
  }
}

Hope that might help. 希望可能有所帮助。

Regards 问候

smtp is not a supported protocol (at least as of 1.5), you would use the mailto protocol. smtp不是受支持的协议(至少从1.5开始),您将使用mailto协议。 See the following example courtesy of google.... 请参阅以下示例礼貌的谷歌....

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

Take care with the firewall and the host port you selected and try the code below that uses javax.mail API. 注意防火墙和您选择的主机端口,然后尝试使用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