目次
hello
ホームページ 类库下载 java类库 JavaMail電子メール開発

JavaMail電子メール開発

Oct 14, 2016 pm 03:41 PM

1. プレーンテキストのみのメール

コード例は次のとおりです:

package com.lyh.sendemail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

//发送邮件
public class MessageDemo1 {

    public static void main(String[] args) throws Exception{
        Properties props = new Properties();//key value:配置参数。真正发送邮件时再配置
        props.setProperty("mail.transport.protocol", "smtp");//指定邮件发送的协议,参数是规范规定的
        props.setProperty("mail.host", "smtp.163.com");//指定发件服务器的地址,参数是规范规定的
//        props.setProperty("mail.debug", "true");//邮件发送的调试模式,参数是规范规定的
        props.setProperty("mail.smtp.auth", "true");//请求服务器进行身份认证。参数与具体的JavaMail实现有关
        
        Session session = Session.getInstance(props);//发送邮件时使用的环境配置
        session.setDebug(true);
        MimeMessage message = new MimeMessage(session);
        
        //设置邮件的头
        message.setFrom(new InternetAddress("xxx@163.com"));
        message.setRecipients(Message.RecipientType.TO, "xxx@qq.com");
        message.setSubject("This is second message");
        //设置正文
        message.setContent("<h1 id="hello">hello</h1>", "text/html");
//        message.setText("<h1 id="hello">hello</h1>");//纯文本
        
        message.saveChanges();
        
        //发送邮件
        Transport ts = session.getTransport();
        ts.connect("xxx@163.com", "123456");       // 密码为授权码不是邮箱的登录密码
        ts.sendMessage(message, message.getAllRecipients());//对象,用实例方法}
    }
}
ログイン後にコピー

2. 画像付きメール

a. 複雑なメールカプセル化モデル

JavaMail電子メール開発

コード例

package com.lyh.sendemail;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

//发送邮件
public class MessageDemo2 {

    public static void main(String[] args) throws Exception{
        Properties props = new Properties();//key value:配置参数。真正发送邮件时再配置
        
        props.setProperty("mail.transport.protocol", "smtp");//指定邮件发送的协议,参数是规范规定的
        props.setProperty("mail.host", "smtp.163.com");//指定发件服务器的地址,参数是规范规定的
//        props.setProperty("mail.debug", "true");//邮件发送的调试模式,参数是规范规定的
        props.setProperty("mail.smtp.auth", "true");//请求服务器进行身份认证。参数与具体的JavaMail实现有关
        
        Session session = Session.getInstance(props);//发送邮件时使用的环境配置
        session.setDebug(true);
        MimeMessage message = new MimeMessage(session);
        
        //设置邮件的头
        message.setFrom(new InternetAddress("xxx@163.com"));
        message.setRecipients(Message.RecipientType.TO, "xxx@qq.com");
        message.setSubject("This is second message");
        //设置正文
        
        //搞出文本部分
        MimeBodyPart textPart = new MimeBodyPart();
        textPart.setContent("aaa<img  src=&#39;cid:mm&#39;/ alt="JavaMail電子メール開発" >aaa", "text/html");
        
        //搞图片部分
        MimeBodyPart imagePart = new MimeBodyPart();
        imagePart.setContentID("mm");
        //把磁盘上的文件加到part中使用到了JAF框架
        DataHandler dh = new DataHandler(new FileDataSource("src/0.jpg"));
        imagePart.setDataHandler(dh);
        
        MimeMultipart mp = new MimeMultipart();
        mp.addBodyPart(textPart);
        mp.addBodyPart(imagePart);
        mp.setSubType("related");//有关系的
        
        message.setContent(mp);
        message.saveChanges();
        
        //发送邮件
        Transport ts = session.getTransport();
        ts.connect("xxx@163.com", "123456"); //密码为授权码不是邮箱的登录密码
        ts.sendMessage(message, message.getAllRecipients());//对象,用实例方法
    }
}
ログイン後にコピー

3.および添付ファイル

コード例:

package com.lyh.sendemail;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

//发送邮件
public class MessageDemo3 {

    public static void main(String[] args) throws Exception{

        Properties props = new Properties();//key value:配置参数。真正发送邮件时再配置
        
        props.setProperty("mail.transport.protocol", "smtp");//指定邮件发送的协议,参数是规范规定的
        props.setProperty("mail.host", "smtp.163.com");//指定发件服务器的地址,参数是规范规定的
//        props.setProperty("mail.debug", "true");//邮件发送的调试模式,参数是规范规定的
        props.setProperty("mail.smtp.auth", "true");//请求服务器进行身份认证。参数与具体的JavaMail实现有关
        
        Session session = Session.getInstance(props);//发送邮件时使用的环境配置
//        session.setDebug(true);
        MimeMessage message = new MimeMessage(session);
        
        //设置邮件的头
        message.setFrom(new InternetAddress("xxx@163.com"));
        message.setRecipients(Message.RecipientType.TO, "xxxqq.com");
        message.setSubject("这是一封复杂的邮件");
        //设置正文
        
        //搞出文本部分
        MimeBodyPart textPart = new MimeBodyPart();
        textPart.setContent("美女<img  src=&#39;cid:mm&#39;/ alt="JavaMail電子メール開発" >aaa", "text/html;charset=UTF-8");
        
        //搞图片部分
        MimeBodyPart imagePart = new MimeBodyPart();
        imagePart.setContentID("mm");
        //把磁盘上的文件加到part中使用到了JAF框架
        DataHandler dh = new DataHandler(new FileDataSource("src/0.jpg"));
        imagePart.setDataHandler(dh);
        
        MimeMultipart mp = new MimeMultipart();
        mp.addBodyPart(textPart);
        mp.addBodyPart(imagePart);
        mp.setSubType("related");//有关系的
        
        MimeBodyPart textImagePart = new MimeBodyPart();    //将 MimeMultipart 添加到 MimeBodyPart实现附件的发送
        textImagePart.setContent(mp);
        
        //创建附件部分
        MimeBodyPart attachmentPart = new MimeBodyPart();
        dh = new DataHandler(new FileDataSource("src/账户.rar"));
        String filename = dh.getName();
        attachmentPart.setDataHandler(dh);
        
        //手工设置文件名  防止乱码使用  javaMail里的 MimeUtility进行编码
        attachmentPart.setFileName(MimeUtility.encodeText(filename)); 
        
        //最终的 MimeMultipart
        MimeMultipart finalMp = new MimeMultipart();
        finalMp.addBodyPart(attachmentPart);
        finalMp.addBodyPart(textImagePart);
        
        finalMp.setSubType("mixed");
        
        message.setContent(finalMp);
        message.saveChanges();
        
        //发送邮件
        Transport ts = session.getTransport();
        ts.connect("xxx@163.com", "123456"); //密码为授权码不是邮箱的登录密码
        ts.sendMessage(message, message.getAllRecipients());//对象,用实例方法
    
    }
}
ログイン後にコピー


このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)