java发送邮件,带跳转链接地址(包含遇见的坑)

java发送邮件带url、带html 、带跳转页面

①创建密码验证器类(不知道干啥用的)

package com.mail.test;
 
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
 
public class MailAuthenticator extends Authenticator {
   
	
	public MailAuthenticator(String userName,String userPwd){
   
		this.userAccout = userName;
		this.userPassword = userPwd;
	}
	
	private String userAccout;
	private String userPassword;
	public String getUserAccout() {
   
		return userAccout;
	}
	public void setUserAccout(String userAccout) {
   
		this.userAccout = userAccout;
	}
	public String getUserPassword() {
   
		return userPassword;
	}
	public void setUserPassword(String userPassword) {
   
		this.userPassword = userPassword;
	}
	
	
	@Override
	protected PasswordAuthentication getPasswordAuthentication() {
   
		// TODO Auto-generated method stub
		return new PasswordAuthentication(userAccout, userPassword);
	}
}

②邮箱实体类(实用性,还是可以的)

package com.mail.test;
 
import java.util.Properties;
 
public class MainInfo {
   
	// 发送邮件的服务器的IP和端口   
    private String mailServerHost;   
    private String mailServerPort = "25";   
    // 邮件发送者的地址   
    private String fromAddress;   
    // 邮件接收者的地址   
    private String toAddress;   
    // 登陆邮件发送服务器的用户名和密码   
    private String userName;   
    private String password;   
    // 是否需要身份验证   
    private boolean validate = false;   
    // 邮件主题   
    private String subject;   
    // 邮件的文本内容   
    private String content;   
    // 邮件附件的文件名   
    private String[] attachFileNames;     
    /**  
      * 获得邮件会话属性  
      */   
    public Properties getProperties(){
      
      Properties p = new Properties();   
      p.put("mail.smtp.host", this.mailServerHost);   
      p.put("mail.smtp.port", this.mailServerPort);   
      p.put("mail.smtp.auth", validate ? "true" : "false");   
      return p;   
    }
	public String getMailServerHost() {
   
		return mailServerHost;
	}
	public void setMailServerHost(String mailServerHost) {
   
		this.mailServerHost = mailServerHost;
	}
	public String getMailServerPort() {
   
		return mailServerPort;
	}
	public void setMailServerPort(String mailServerPort) {
   
		this.mailServerPort = mailServerPort;
	}
	public String getFromAddress() {
   
		return fromAddress;
	}
	public void setFromAddress(String fromAddress) {
   
		this.fromAddress = fromAddress;
	}
	public String getToAddress() {
   
		return toAddress;
	}
	public void setToAddress(String toAddress) {
   
		this.toAddress = toAddress;
	}
	public String getUserName() {
   
		return userName;
	}
	public void setUserName(String userName) {
   
		this.userName = userName;
	}
	public String getPassword() {
   
		return password;
	}
	public void setPassword(String password) {
   
		this.password = password;
	}
	public boolean isValidate() {
   
		return validate;
	}
	public void setValidate(boolean validate) {
   
		this.validate = validate;
	}
	public String getSubject() {
   
		return subject;
	}
	public void setSubject(String subject) {
   
		this.subject = subject;
	}
	public String getContent() {
   
		return content;
	}
	public void setContent(String content) {
   
		this.content = content;
	}
	public String[] getAttachFileNames() {
   
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是Java发送附件的邮件的示例代码: ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendEmailWithAttachment { public static void main(String[] args) { // 收件人邮箱地址 String to = "recipient@example.com"; // 发件人邮箱地址 String from = "sender@example.com"; // 发件人邮箱密码或授权码 String password = "your_password_or_auth_code"; // 邮件主题和正文 String subject = "Test email with attachment"; String body = "Please see the attached file."; // 创建邮件会话 Properties props = new Properties(); props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); try { // 创建邮件消息 MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); // 创建邮件正文 MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(body); // 创建邮件附件 MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.attachFile("path/to/attachment"); // 将正文和附件添加到邮件消息中 Multipart multipart = new MimeMultipart(); multipart.addBodyPart(textPart); multipart.addBodyPart(attachmentPart); message.setContent(multipart); // 发送邮件 Transport.send(message); System.out.println("Email sent successfully."); } catch (Exception e) { System.err.println("Failed to send email: " + e.getMessage()); } } } ``` 在上面的代码中,需要将以下信息替换为实际的值: - `to`:收件人邮箱地址 - `from`:发件人邮箱地址 - `password`:发件人邮箱密码或授权码 - `subject`:邮件主题 - `body`:邮件正文 - `props.put("mail.smtp.host", "smtp.example.com")`:SMTP服务器地址 另外,需要将附件的文件路径替换为实际的路径。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值