gpt4 book ai didi

java - 如何在使用模糊域时从 Java 应用程序发送电子邮件?

转载 作者:太空宇宙 更新时间:2023-11-04 13:39:04 25 4
gpt4 key购买 nike

我正在尝试从使用 hostmonster 域的电子邮件帐户发送电子邮件。我一直在使用我在另一篇文章中找到的代码,该代码非常适合 gmail,但我在调整它以适应这个域时遇到了困难。原代码为:

Properties props = System.getProperties();
String host = "smpt.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");

从此我更改了主机和端口以适合我的域。主机为“host407.hostmonster.com”,传出 smtp 端口为 465。当我使用这些更改运行应用程序时,没有任何反应。我没有收到代码无法连接到服务器、密码错误或任何其他反馈的错误,它只是继续运行,而当我将它与 gmail 一起使用时,运行应用程序并发送几封电子邮件需要两到三秒的时间。我完全不知道该尝试什么,而且我在这里找不到任何关于使用除大名称域名之外的电子邮件的帖子,因此我们将不胜感激!

编辑:我应该包含我自己的所有代码,抱歉,这里是:

   import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Formater {

private static String USER_NAME = "***********"; // GMail user name (just the part before "@gmail.com")
private static String PASSWORD = "********"; // GMail password
private static String[] RECIPIENTS = { "***************", "*************"}; //"******************",
private static String WeatherText = "This is where the weather text will be and how it will look.";
private static File outputFolder = new File("C:/Users/***** *******/Documents/**********/Output/Test.txt");
private static ArrayList<String> Text = new ArrayList<>();


public static void main(String[] args) throws FileNotFoundException {
String from = USER_NAME;
String pass = PASSWORD;
String[] to = { "**************"}; // list of recipient email addresses
String[] bcc = RECIPIENTS;
String subject = "Java send mail example";
String body = "Welcome to JavaMail!";
sendFromMail(from, pass, to, bcc, subject, body);

}

private static void sendFromMail(String from, String pass, String[] to, String[] bcc, String subject, String body) throws FileNotFoundException {
Properties props = System.getProperties();
String host = "host407.hostmonster.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");


Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);

try {
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
InternetAddress[] bccAddress = new InternetAddress[bcc.length];

// To get the array of addresses
for( int i = 0; i < to.length; i++ ) {
toAddress[i] = new InternetAddress(to[i]);
}

for( int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}

for( int i = 0; i <bcc.length;i++){
bccAddress[i] = new InternetAddress(bcc[i]);
}

for( int i = 0; i<bccAddress.length; i++){
message.addRecipient(Message.RecipientType.BCC, bccAddress[i]);
}
message.setSubject(subject);
//message.setText(body);
message.setContent("<!DOCTYPE html>\n" +

最佳答案

好吧,我感到无聊,开始尝试随机端口,看看是否有一个可以工作,而我尝试的第三个端口(587)工作了。

关于java - 如何在使用模糊域时从 Java 应用程序发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31394038/

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