在 Java 中使用 MIME 的自定义邮件标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12511744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:10:51  来源:igfitidea点击:

Custom mail headers using MIME in Java

javaemailemail-headers

提问by ganga

I want to create a new custom header while sending email, but by using setHeader()and addHeader()methods I am unable to do it.

我想在发送电子邮件时创建一个新的自定义标题,但通过使用setHeader()addHeader()方法我无法做到。

How can I create a user defined X-""email header?

如何创建用户定义的X-""电子邮件标头?

回答by swemon

setHeader() works for me. In below case, I set encoding options to mail header.

setHeader() 对我有用。在下面的情况下,我将编码选项设置为邮件标题。

String mail_body = "<html><head></head><body><h1>Mail Body</h1><b>This is mail body of Test mail.</b></body></html>";
String encodingOptions = "text/html; charset=UTF-8";

MimeMessage message = new MimeMessage(session);
message.setContent("Hello", "text/plain");
message.setSubject(mail_subject);
message.setText(mail_body);
message.setHeader("Content-Type", encodingOptions);
message.setSentDate(new Date());

You can see message header in receiving email with https://support.google.com/mail/bin/answer.py?hl=en&answer=22454

您可以通过https://support.google.com/mail/bin/answer.py?hl=zh-CN&answer=22454在接收电子邮件时看到邮件标题

Sample program. Mail.java

示例程序。邮件.java

package com.test.mail;

import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail {
    private String host = "smtp.gmail.com";

    private String mail_to = "[email protected]";

    private String mail_from = "[email protected]";// using gmail server

    private String mail_subject = "Subject of this test mail";

    private String mail_body = "<html><head></head><body><h1>Mail Body</h1><b>This is mail body of Test mail.</b></body></html>";

    private String personalName = "xxx";

    private String encodingOptions = "text/html; charset=UTF-8";

    public void sendMail() throws SendFailedException {
        try {

            Properties props = new Properties();
            Authenticator auth = new Email_Autherticator();

            //props.put("mail.smtp.host", host);
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "587");
            System.out.println(props);

            Session session = Session.getDefaultInstance(props, auth);

            MimeMessage message = new MimeMessage(session);
            message.setContent("Hello", "text/plain");
            message.setSubject(mail_subject);
            message.setText(mail_body);
            message.setHeader("Content-Type", encodingOptions);
            //message.setHeader(mail_head_name, mail_head_value);
            message.setSentDate(new Date());

            Address address = new InternetAddress(mail_from, personalName);
            message.setFrom(address);

            Address toaddress = new InternetAddress(mail_to);
            message.addRecipient(Message.RecipientType.TO, toaddress);

            System.out.println(message);

            Transport.send(message);
            System.out.println("Send Mail Ok!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        // return flag;
    }

    public static void main(String args[]) {
        try {
            Mail mailObj = new Mail();
            mailObj.sendMail();
        } catch (Exception e) {

        }
    }
}

Email_Autherticator.java

Email_Autherticator.java

package com.test.mail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class Email_Autherticator extends Authenticator {
    String username = "[email protected]";

    String password = "password";

    public Email_Autherticator() {
        super();
    }

    public Email_Autherticator(String user, String pwd) {
        super();
        username = user;
        password = pwd;
    }

    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }
}