java 通过javax.mail发送电子邮件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 通过javax.mail发送电子邮件相关的知识,希望对你有一定的参考价值。

package mail;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailDemo1 {

    public static void main(String[] args) {
        final String receiver = "receiveraddress@sample.org";
        final String sender = "senderaddress@sample.org";
        final String host = "localhost";

        final Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", host);

        final MimeMessage message = new MimeMessage(Session.getDefaultInstance(properties));

        try {
            message.setFrom(new InternetAddress(sender));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiver));
            message.setSubject("Email test");
            message.setContent("<h1>Yeah!</h1><p>This is a simple HTML-Email.</p>", "text/html");

            Transport.send(message);
            System.out.println("Message sent successfully.");
        } catch (MessagingException e) {
            System.err.println("Message did not sent successfully.");
            e.printStackTrace();
        }
    }
}

以上是关于java 通过javax.mail发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

JAVA使用javax.mail发送邮件

java使用javax.mail进行免费的邮件发送

Java使用javax.mail.jar发送邮件并同意发送附件

在 javax.mail 中发送邮件,无需身份验证

使用javax.mail实现邮件的发送

使用javax.mail实现邮件的发送