发送代码java 发送代码的次数过多,两天了( 二 )


回答于 2022-12-11
求java实现邮件发送的源代码import java.util.*;
import javax.mail.*;import javax.mail.internet.*;
public class JMail {
public void SendMail(String Topic,String Content){Properties props=new Properties();props.put("mail.smtp.host","smtp.163.com");props.put("mail.smtp.auth","true");Session s=Session.getInstance(props);s.setDebug(false);MimeMessage message=new MimeMessage(s);MimeMultipart mp=new MimeMultipart();BodyPart body = new MimeBodyPart();InternetAddress from;InternetAddress to;try{from=new InternetAddress("发件人邮箱");message.setFrom(from);to = new InternetAddress("收件人邮箱");message.setRecipient(Message.RecipientType.TO,to);message.setSubject(Topic,"utf-8");body.setContent(Content, "text/html;charset=utf-8");mp.addBodyPart(body);message.setContent(mp);message.setSentDate(new Date());message.saveChanges();Transport transport=s.getTransport("smtp");transport.connect("smtp.163.com(邮件服务商,这是163的)","发件邮箱","发件邮箱密码");transport.sendMessage(message,message.getAllRecipients());transport.close();}catch(AddressException e){e.printStackTrace();}catch(MessagingException e){e.printStackTrace();}}}
如何使用Java发送qq邮件方法:
1.前提准备工作:
首先 , 邮件的发送方要开启POP3 和SMTP服务--即发送qq邮件的账号要开启POP3 和SMTP服务
2.开启方法:
登陆qq邮箱
3.点击设置
4.点击—-账户
5.找到:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 —点击开启
6.送短信 —–点击确定
7.稍等一会,很得到一个授权码! –注意:这个一定要记住,一会用到
8.点击保存修改 —OK 完成
9.java 测试代码:
package cn.cupcat.test;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class SendmailUtil {
public static void main(String[] args) throws AddressException,MessagingException {
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 连接协议
properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
properties.put("mail.smtp.port", 465);// 端口号
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");//设置是否使用ssl安全连接---一般都使用
properties.put("mail.debug", "true");//设置是否显示debug信息true 会在控制台显示相关信息
//得到回话对象
Session session = Session.getInstance(properties);
// 获取邮件对象
Message message = new MimeMessage(session);
//设置发件人邮箱地址
message.setFrom(new InternetAddress("123456789@qq.com"));
//设置收件人地址message.setRecipients(RecipientType.TO,new InternetAddress[] { new InternetAddress("987654321@qq.com") });
//设置邮件标题
【发送代码java 发送代码的次数过多,两天了】message.setSubject("这是第一封Java邮件");
//设置邮件内容
message.setText("内容为: 这是第一封java发送来的邮件 。");
//得到邮差对象
Transport transport = session.getTransport();
//连接自己的邮箱账户
transport.connect("123456789@qq.com", "vvctybgbvvophjcj");//密码为刚才得到的授权码
//发送邮件transport.sendMessage(message, message.getAllRecipients());
}
}
10.运行就会发出邮件了 。。。。
下面是我收到邮件的截图,当然我把源码中的邮件地址都是修改了,不是真实的 , 你们测试的时候,可以修改能你们自己的邮箱 。最后 , 祝你也能成功,如果有什么问题,可以一起讨论!

推荐阅读