简体   繁体   中英

How do you send email from a java application while using an obscure domain?

I am trying to send an email from an email account that uses a hostmonster domain. I have been using code that I found in another post that works great for gmail but I am having trouble adapting it to suit this domain. The original code is:

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");

From this I have changed the host and the port to fit my domain. The host being "host407.hostmonster.com" and the outgoing smtp port being 465. When I run my application with these changes nothing happens. I don't get an error that my code couldn't connect to a server, the password was wrong, or any other feedback, it just keeps running while when I used it with gmail it took between two and three seconds to run the application and send several emails. I am completely at a loss for what to try and I haven't been able to find any posts here about using an email other than big name domains, so any help will be appreciated!

Edit: I should have included all of my own code sorry, here it is:

   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)可行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM