gpt4 book ai didi

java - 邮件发送问题

转载 作者:行者123 更新时间:2023-11-30 05:57:14 25 4
gpt4 key购买 nike

public class MailEx {
public static void main(String[] args) {
try {

String userName = "abc@gmail.com";
String password = "123";
String hostName = "smtp.gmail.com";
String fromName = "Splendore Bkk";
String to[] = {"xyz@gmail.com"};

System.out.println("to.length::"+to.length);

Properties props = new Properties();
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
System.out.println("to.length:sadfsadfds:"+to.length);
// Get the default Session object.
Session session = Session.getInstance(props);

// Create a default MimeMessage object.
MimeMessage message1 = new MimeMessage(session);

// Set the RFC 822 "From" header field using the
// value of the InternetAddress.getLocalAddress method.
message1.setFrom(new InternetAddress(userName,fromName));

Address[] addresses = new Address[to.length];
for (int i = 0; i < to.length; i++) {
Address address = new InternetAddress(to[i]);
addresses[i] = address;
// Add the given addresses to the specified recipient type.
message1.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
}
// Set the "Subject" header field.
message1.setSubject("Testing");

// Sets the given String as this part's content,
// with a MIME type of "text/plain".
Multipart mp = new MimeMultipart("alternative");
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent("Hii from cc", "text/html");
mp.addBodyPart(mbp);
message1.setContent(mp);
message1.saveChanges();

// Send message
Transport transport = session.getTransport("smtp");
transport.connect(hostName,userName,password);
transport.sendMessage(message1,addresses);
transport.close();


}catch (Exception e) {
e.printStackTrace();
}

}

}

我收到错误....

DEBUG: JavaMail version 1.4ea
DEBUG: java.io.FileNotFoundException: ..\Java\jdk1.6.0\jre\lib\javamail.providers (The system cannot find the file specified)
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.providers
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
at javax.mail.Session.loadProvidersFromStream(Session.java:928)
at javax.mail.Session.access$000(Session.java:174)
at javax.mail.Session$1.load(Session.java:870)
at javax.mail.Session.loadResource(Session.java:1084)
at javax.mail.Session.loadProviders(Session.java:889)
at javax.mail.Session.<init>(Session.java:210)
at javax.mail.Session.getInstance(Session.java:249)
at com.test.MailEx.main(MailEx.java:41)

那你能告诉我问题是什么吗...

最佳答案

为了避免 DEBUG 警告,创建文件 javamail.providers, javamail.address.map, javamail.default.address.map, javamail.default.providers under

 (Program Files)\Java\jdk1.6.0\jre\lib\

文件夹。

关于错误,NoClassDefFoundError,您只是没有将 JavaMail 添加到类路径中。如果您使用的是 Eclipse,请右键单击项目,按照 Build Path ≥ Add Libraries 或类似的方式添加 javamail 的 jar 文件(您应该位于 lib/ 文件夹下)到你的项目的类路径。

关于java - 邮件发送问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6081270/

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