java gmail 发送邮件_Java通过Gmail发送电子邮件

大家好,我刚刚尝试获取一些Java代码,以通过gmail向Java用户发送电子邮件,这就是我所拥有的:

@ManagedBean

@ViewScoped

public class email {

// Set up the SMTP server.

java.util.Properties props = new java.util.Properties();

public void mail() {

System.out.println("Called mail");

props.put(

"mail.smtp.gmail", "smtp.gmail.com");

props.put("mail.smtp.port", "587");

Session session = Session.getDefaultInstance(props, null);

// Construct the message

String to = "[email protected]";

String from = "[email protected]";

String subject = "Hello";

Message msg = new MimeMessage(session);

try {

System.out.println("Setting up the email");

msg.setFrom(new InternetAddress(from));

msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));

System.out.println("here");

msg.setSubject("Print Job");

msg.setText("Hi,

How are you?");

Transport.send(msg);

System.out.println("Sending message"); //does not get to this part

} catch (MessagingException e) {

// Error.

}

}

}

它在运行等时没有错误,我在控制台中没有收到发送消息,而只是Transport.send(msg)阻塞了我做错了什么? gmail的设置都可以吗?

编辑:

@ManagedBean

@ViewScoped

public class email {

// Set up the SMTP server.

java.util.Properties props = new java.util.Properties();

public void mail() {

System.out.println("Called mail");

String host = "smtp.gmail.com";

String from = "------------";

String pass = "----";

props.put("mail.smtp.starttls.enable", "true"); // added this line

props.put("mail.smtp.host", host);

props.put("mail.smtp.user", from);

props.put("mail.smtp.password", pass);

props.put("mail.smtp.port", "587");

props.put("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(props,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
发送企业邮箱,可以使用Java Mail API。以下是一个简单的示例代码,您可以使用它来发送邮件。 ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String[] args) { String to = "[email protected]"; // 收件人电子邮箱 String from = "[email protected]"; // 发件人电子邮箱 String password = "yourpassword"; // 发件人邮箱密码 // 指定发送邮件的主机为 smtp.qq.com String host = "smtp.qq.com"; // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); properties.setProperty("mail.smtp.auth", "true"); // 获取默认的 Session 对象 Session session = Session.getDefaultInstance(properties, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); try { // 创建默认的 MimeMessage 对象 MimeMessage message = new MimeMessage(session); // 设置 From: 头部头字段 message.setFrom(new InternetAddress(from)); // 设置 To: 头部头字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置主题 message.setSubject("测试邮件"); // 设置消息体 message.setText("这是一封测试邮件。"); // 发送消息 Transport.send(message); System.out.println("邮件已经发送。"); } catch (MessagingException mex) { mex.printStackTrace(); } } } ``` 请注意,您需要将以下变量替换为您的实际值: - to:收件人的电子邮件地址 - from:发件人的电子邮件地址 - password:发件人的电子邮件密码 - host:SMTP服务器主机名(例如,如果您使用Gmail,则为“smtp.gmail.com”) 此外,请确保在运行此代码之前,在您的计算机上安装了Java Mail API。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值