SUN公司JavaMail发送邮件

JavaEmail邮件发送

/**
 * @author Administrator
 * 演示SUN公司的JavaMail发送邮件
 */
public class Demo01 {
	public static void main(String[] args) throws Exception{
		
		//封装客户端与易邮邮件服务器的通信协议和主机域名或IP地址
		Properties props = new Properties();
		props.setProperty("mail.host","127.0.0.1");
		props.setProperty("mail.transport.protocol","smtp");
		
		//获取Session
		Session session = Session.getDefaultInstance(props);
		
		//创建一封邮件
		Message message = new MimeMessage(session);
		
		//设置邮件的相关信息
		message.setFrom(new InternetAddress("[email protected]"));
	
		message.setRecipient(RecipientType.TO,new InternetAddress("[email protected]"));
		
		message.setSubj
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java中使用JavaMail库匿名发送邮件,你可以按照以下步骤进行操作: 1. 首先,确保你已经导入了javamail库。你可以在Maven项目中添加以下依赖项: ```xml <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> ``` 2. 创建一个`Properties`对象来配置SMTP服务器的连接属性。设置SMTP服务器的主机名和端口号,以及启用SSL加密(如果需要): ```java Properties properties = new Properties(); properties.put("mail.smtp.host", "your_smtp_host"); properties.put("mail.smtp.port", "your_smtp_port"); properties.put("mail.smtp.ssl.enable", "true"); // 如果需要加密,将其设置为"true" ``` 3. 创建一个`Session`对象,用于建立与SMTP服务器的连接。在创建`Session`对象时,可以通过传递`Authenticator`对象来实现匿名发送邮件: ```java Session session = Session.getInstance(properties, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("", ""); // 空的用户名和密码 } }); ``` 4. 创建一个`Message`对象,用于构造邮件。设置发件人、收件人、主题和内容等信息: ```java Message message = new MimeMessage(session); message.setFrom(new InternetAddress("sender@example.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com")); message.setSubject("Hello, World!"); message.setText("This is the content of the email."); ``` 5. 使用`Transport`类发送邮件: ```java Transport.send(message); ``` 这样,你就可以使用JavaMail库在内网中匿名发送邮件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值