我正在使用以下代码,以便在我的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的协议名称是什么?
答案 0 :(得分: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 = "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);
}
}
希望这可能会有所帮助。
此致
答案 1 :(得分:1)
smtp不是受支持的协议(至少从1.5开始),您将使用mailto协议。请参阅以下示例,由google ....
提供http://www.java2s.com/Code/Java/Network-Protocol/sendsemailusingamailtoURL.htm
答案 2 :(得分: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();
}
}