在使用不明显的域时,如何从Java应用程序发送电子邮件?

时间:2015-07-13 21:43:54

标签: java email

我正在尝试从使用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" +

1 个答案:

答案 0 :(得分:0)

好吧,我感到无聊,刚开始尝试随机端口,看看是否可以使用,第三个端口(587)我试过了。