gpt4 book ai didi

java - 尝试使用 java 发送邮件时获取 javax.mail.NoSuchProviderException

转载 作者:搜寻专家 更新时间:2023-10-31 19:38:03 24 4
gpt4 key购买 nike

我已经阅读并尝试了 stackoverflow 和其他各种站点中提供的所有解决方案,但仍然遇到问题和异常。

代码:

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class sendmail {

Properties emailProperties;
Session mailSession;
MimeMessage emailMessage;

public static void main(String args[]) throws AddressException,
MessagingException {

sendmail javaEmail = new sendmail();

javaEmail.setMailServerProperties();
javaEmail.createEmailMessage();
javaEmail.sendEmail();
}

public void setMailServerProperties() {

String emailPort = "587";//gmail's smtp port

emailProperties = System.getProperties();
emailProperties.put("mail.smtp.port", emailPort);
emailProperties.put("mail.smtp.auth", "true");
emailProperties.put("mail.smtp.starttls.enable", "true");

}

public void createEmailMessage() throws AddressException,
MessagingException {
String[] toEmails = { "emailid@gmail.com" };
String emailSubject = "Java Email";
String emailBody = "This is an email sent by JavaMail api.";

mailSession = Session.getDefaultInstance(emailProperties, null);
emailMessage = new MimeMessage(mailSession);

for (int i = 0; i < toEmails.length; i++) {
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i]));
}

emailMessage.setSubject(emailSubject);
emailMessage.setContent(emailBody, "text/html");//for a html email
//emailMessage.setText(emailBody);// for a text email

}

public void sendEmail() throws AddressException, MessagingException {

String emailHost = "smtp.gmail.com";
String fromUser = "emailid";//just the id alone without @gmail.com
String fromUserEmailPassword = "test";

Transport transport = mailSession.getTransport("smtp");

transport.connect(emailHost, fromUser, fromUserEmailPassword);
transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
transport.close();
System.out.println("Email sent successfully.");
}

}

当我调试代码时,它在以下行停止工作:Transport transport = mailSession.getTransport("smtp");

我添加了以下 JAR:

Mail.jar , pop3.jar , smtp-1.4.2.jar , Activation.jar , additional.jar

完全异常:

Exception in thread "main" javax.mail.NoSuchProviderException: smtp at javax.mail.Session.getService(Session.java:764) at javax.mail.Session.getTransport(Session.java:689) at javax.mail.Session.getTransport(Session.java:632) at javax.mail.Session.getTransport(Session.java:612) at javax.mail.Session.getTransport(Session.java:667) at javax.mail.Transport.send0(Transport.java:154) at javax.mail.Transport.send(Transport.java:80) at JannyaPaid_Device.sendmail.sendEmail(sendmail.java:68) at JannyaPaid_Device.sendmail.main(sendmail.java:26)

另外我想问一下,防火墙可以阻止这种东西发邮件吗?由于我们安装了一些防火墙,但我可以通过 gmail 手动打开和发送邮件。

最佳答案

import java.util.Properties;
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 SendMailTLS {

public static void main(String[] args) {

final String username = "username@gmail.com";
final String password = "password";

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

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to-email@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");

Transport.send(message);

System.out.println("Done");

} catch (MessagingException e) {
throw new RuntimeException(e);
}
}

关于java - 尝试使用 java 发送邮件时获取 javax.mail.NoSuchProviderException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30472422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com