java 发送邮件带附件

首先开启

使用的 qq 邮箱

在这里插入图片描述
开启这两项服务,需要发送短信验证,验证成功会生成一个授权码,没有必要记下授权码,可之后单独生成。
在这里插入图片描述

须知

在邮件服务器官网会有提供,此处点击 IMAP 服务 后面的超链接即可查看。
在这里插入图片描述

引入依赖

<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<!--发送邮件-->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.5</version>
</dependency>

先查看源码 Demo

没有什么比源码更能助人理解的了
在这里插入图片描述


import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;

import javax.mail.*;
import javax.mail.internet.*;

import com.sun.mail.smtp.*;

/**

 * 演示应用程序,显示了如何构造和发送RFC822
 * (singlepart)消息。
 *
 * XXX - 允许命令行上有多个接收者
 *
 * 这只是mssend .java的一个变体,演示使用
 * 一些特定于smtp的特性。
 *
 * @author Max Spivak
 * @author Bill Shannon
 */

public class smtpsend {
   

    /**
     * Example of how to extend the SMTPTransport class.
     * This example illustrates how to issue the XACT
     * command before the SMTPTransport issues the DATA
     * command.
     *
    public static class SMTPExtension extends SMTPTransport {
	public SMTPExtension(Session session, URLName url) {
	    super(session, url);
	    // to check that we're being used
	    System.out.println("SMTPExtension: constructed");
	}

	protected synchronized OutputStream data() throws MessagingException {
	    if (supportsExtension("XACCOUNTING"))
		issueCommand("XACT", 250);
	    return super.data();
	}
    }
     */

    public static void main(String[] argv) {
   
	String  to, subject = null, from = null, 
		cc = null, bcc = null, url = null;
	String mailhost = null;
	String mailer = "smtpsend";
	String file = null;
	String protocol = null, host = null, user = null, password = null;
	String record = null;	// name of folder in which to record mail
	
	boolean debug = false;
	boolean verbose = false;
	boolean auth = false;
	String prot = "smtp";
	BufferedReader in =
			new BufferedReader(new InputStreamReader(System.in));
	int optind;

	/*
	 * Process command line arguments.
	 */
	for (optind = 0; optind < argv.length; optind++) {
   
	    if (argv[optind].equals("-T")) {
   
		protocol = argv[++optind];
	    } else if (argv[optind].equals("-H")) {
   
		host = argv[++optind];
	    } else if (argv[optind].equals("-U")) {
   
		user = argv[++optind];
	    } else if (argv[optind].equals("-P")) {
   
		password = argv[++optind];
	    } else if (argv[optind].equals("-M")) {
   
		mailhost = argv[++optind];
	    } else if (argv[optind].equals("-f")) {
   
		record = argv[++optind];
	    } else if (argv[optind].equals("-a")) {
   
		file = argv[++optind];
	    } else if (argv[optind].equals("-s")) {
   
		subject = argv[++optind];
	    } else if (argv[optind].equals("-o")) {
    // originator
		from = argv[++optind];
	    } else if (argv[optind].equals("-c")) {
   
		cc = argv[++optind];
	    } else if (argv[
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值