使用java发送email

必须下载sun公司的JavaMail API包,地址为: http://java.sun.com/products/javamail/ 并将相关包(jar文件)加到CLASSPATH中。
或者如果安装j2ee则将j2ee.jar加入classpath即可。
CODE:


import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
/**
*
Title: java mail

*
Description:

*
Copyright: Copyright (c) 2004

*
Company:

* @version 1.0
*/
public class SendMail
{
    public SendMail()
    {
    }
    public void send(){
        try
        {
            Properties props = new Properties();
            Session sendMailSession;
            Store store;
            Transport transport;
            props.put("mail.smtp.auth","true");
            props.put("mail.smtp.host", "smtp.yourmail.com"); //smtp主机名。
            props.put("mail.smtp.user","username@yourmail.com"); //发送方邮件地址。
            props.put("mail.smtp.password","888888"); //邮件密码。
            PopupAuthenticator popA=new PopupAuthenticator();//邮件安全认证。
            PasswordAuthentication pop = popA.performCheck("username","888888"); //填写用户名及密码
            sendMailSession = Session.getInstance(props, popA);
            Message newMessage = new MimeMessage(sendMailSession);
            newMessage.setFrom(new InternetAddress("username@yourmail.com"));
            newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("receiver@mail.com"));  //接收方邮件地址
            newMessage.setSubject("邮件主题");
            newMessage.setSentDate(new Date());
            String mailContent;
            mailContent="你好!/n/n";
            mailContent+="/t邮件正文/n/n";
            mailContent+=new Date().toLocaleString();
            newMessage.setText(mailContent); //邮件正文
            transport = sendMailSession.getTransport("smtp");
            transport.send(newMessage);
        }
        catch (MessagingException ex)
        {
            ex.printStackTrace();
        }
    }
    public static void main(String[] args)
    {
        SendMail sml = new SendMail();
        sml.send();
    }
    public class PopupAuthenticator extends Authenticator{
        String username=null;
        String password=null;
        public PopupAuthenticator(){}
        public PasswordAuthentication performCheck(String user,String pass){
            username = user;
            password = pass;
            return getPasswordAuthentication();
        }
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java发送邮件可以使用JavaMail API。下是一个简单的示例代码,演示如何使用JavaMail API发送邮件: ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String[] args) { // 邮件发送者的邮箱地址和密码 String senderEmail = "your_email@example.com"; String senderPassword = "your_password"; // 邮件接收者的邮箱地址 String recipientEmail = "recipient_email@example.com"; // 邮件服务器的主机名和端口号 String host = "smtp.example.com"; int port = 587; // 创建Properties对象,设置邮件服务器的配置信息 Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); // 创建Session对象,用于与邮件服务器进行通信 Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(senderEmail, senderPassword); } }); try { // 创建MimeMessage对象,设置邮件内容 MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(senderEmail)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipientEmail)); message.setSubject("Hello, World!"); message.setText("This is a test email from Java."); // 发送邮件 Transport.send(message); System.out.println("Email sent successfully."); } catch (MessagingException e) { e.printStackTrace(); } } } ``` 请注意,上述代码中的`your_email@example.com`和`your_password`需要替换为实际的发件人邮箱地址和密码,`recipient_email@example.com`需要替换为实际的收件人邮箱地址,`smtp.example.com`需要替换为实际的邮件服务器主机名。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值