java.net.MalformedURLException smtp协议
为了在java应用程序中使用smtp,我使用了以下代码java.net.MalformedURLException smtp协议,java,smtp,Java,Smtp,为了在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使用的协议名称是什么?smtp不是受支持的协议(至少从1.5开始),您
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使用的协议名称是什么?smtp不是受支持的协议(至少从1.5开始),您将使用mailto协议。请参阅谷歌提供的以下示例
如果您试图通过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);
}
}
希望这能有所帮助
注意小心防火墙和您选择的主机端口,并尝试下面使用API的代码
您正在尝试发送电子邮件吗?我不会直接尝试发送,而是使用可用的第三方库之一,例如,与。没有任何第三方,我们就不能获得它吗libraries@Downvoter:请注意指定相同的原因。很高兴听到你反对答案的论点,我一定会从中吸取教训。
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();
}
}