java如何通过任何帐户发送电子邮件

标签 java jakarta-mail

我可以使用下面的代码发送电子邮件。但我需要根据用户拥有的帐户类型更改 mail.smtp.host 和 mail.smtp.port。

我正在创建一个应用程序,我希望用户只需提供他的电子邮件地址和密码,然后代码就会计算出详细信息。有没有办法做到这一点?我可以尝试从电子邮件地址中找出帐户类型,但这并不总是显而易见的。

 import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.nio.ByteBuffer;
    import java.nio.charset.Charset;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.Properties;
    import java.util.Scanner;

import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailProjectClass {
    //final static String username = "info@mail.com";
    //final static String password = "xxxx";
    final static String username = "yyyy@gmail.com";
    final static String password = "yyyy";
    static Transport bus;

public static void main(String[] args) {



    Properties props = new Properties();
    props.put("mail.smtp.auth", true);
    props.put("mail.smtp.starttls.enable", true);

    //props.put("mail.smtp.host", "smtpout.europe.secureserver.net");
    //props.put("mail.smtp.port", "80"); //25, 3535, 80    465    
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587"); //25, 3535, 80    465

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


        sendEmail(session,"zzzz@wherever.com");


  }

    public static void sendEmail(Session session, String emailAddress)  {

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO,  InternetAddress.parse(emailAddress));
            message.setSubject("Homework");

            Multipart multipart = new MimeMultipart("alternative");

            MimeBodyPart messageBodyPart = new MimeBodyPart();


            String content = "Blank";
            try {

                content = readFile("C:\\Letter.txt", Charset.defaultCharset());
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            messageBodyPart.setContent(content, "text/plain"); 
            messageBodyPart.setDisposition(Part.INLINE);
            multipart.addBodyPart(messageBodyPart);

            try {

                content = readFile("C:\\Letter.htm", Charset.defaultCharset());
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            messageBodyPart.setContent(content, "text/html"); 
            messageBodyPart.setDisposition(Part.INLINE);
            multipart.addBodyPart(messageBodyPart);


            message.setContent(multipart);

            Transport.send(message);


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

编辑1

如果我确实想自己编写所需的大部分详细信息 - 是否有一个 java 类 可以为我进行对话 - 即允许根据帐户类型获取所需的所有不同信息?

最佳答案

你不能这样做,否则会有数以百万计的网站提供你访问你的电子邮件,无论它在哪里,仅用户名和密码不足以确定你需要哪个 SMTP,除非你通过解析电子邮件地址来覆盖已知的 SMTP这不是您正在寻找的。

关于java如何通过任何帐户发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20492728/

相关文章:

java - 如何在 Spring 3 中上传图像并将 JSON 发布到 RESTful Web 服务

java - 约书亚布洛赫 #Item 1 : Consider static factory methods instead of constructors

java - JSP 的分页问题

java - 错误 - trustAnchors 参数必须非空

java - JPA:分离后保留(用于创建实体副本)会混淆 EntityManager 缓存

java - 由于 NoSuchProviderException smtp 无法发送电子邮件

Java - 如何验证用户名和密码

google-calendar-api - 如何使用 BiWeekly 库和 Java Mail API 创建现有事件并发送更新?

java - MimeMessageParser 无法从地址获取

java - 实践中的约束布局看起来与 Android Studio 中不同