test mail

package com.mail;

 

import java.net.MalformedURLException;

import java.net.URL;

 

import javax.mail.MessagingException;

 

import org.apache.commons.mail.EmailAttachment;

import org.apache.commons.mail.EmailException;

import org.apache.commons.mail.HtmlEmail;

import org.apache.commons.mail.MultiPartEmail;

import org.apache.commons.mail.SimpleEmail;

 

/**

 * author:pangxinhua date: 2010-10-29

 */

public class TestMail {

public static void main(String[] args) throws EmailException,

MessagingException, MalformedURLException {

multipartEmail();

}

 

public static void multipartEmail() throws EmailException, MalformedURLException {

/*设置附件start---------------*/

EmailAttachment attachment = new EmailAttachment();

 

//附件路径

attachment.setPath("C://Documents and Settings//pangxinhua//桌面//monitor.sh");

 

//设置为附件

attachment.setDisposition(EmailAttachment.ATTACHMENT);

 

//描述

//attachment.setDescription("Picture of John");

//设置文件名(默认原名)

attachment.setName("monitor.sh");

/*设置附件结束*/

// Create the email message

MultiPartEmail email = new MultiPartEmail();

//服务器

email.setHostName("smtpcom.263xmail.com");

//验证

email.setAuthentication("automail@into4g.com", "into123456");

//发件人

email.setFrom("pangxinhua@into4g.com", "pangxinhua");

//收件人

email.addTo("pangxinhua@into4g.com", "pangxinhua");

//编码

email.setCharset("UTF-8");

//主题

email.setSubject("The picture");

//正文

email.setMsg("Here is the picture you wanted");

 

//添加附件

email.attach(attachment);

//发送

email.send();

 

}

 

public static void htmlEmail() throws EmailException, MalformedURLException {

HtmlEmail m = new HtmlEmail();

m.setAuthentication("automail@into4g.com", "into123456");

m.setHostName("smtpcom.263xmail.com");

m.setFrom("pangxinhua@into4g.com", "pangxinhua");

m.addTo("pangxinhua@into4g.com", "pangxinhua");

// m.setMsg("java mail test");

// m.setSubject("subject");

// //设置主题的字符集为UTF-8

m.setCharset("UTF-8");

m.setSubject("测试邮件主题");

URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");

 

String cid = m.embed(url, "Apache logo");

 

// set the html message

 

m.setHtmlMsg("<html>The apache logo - <img src=/"cid:" + cid

+ "/"></html>");

 

// set the alternative message

 

m.setTextMsg("Your email client does not support HTML messages");

 

// send the email

 

m.send();

}

 

public static void simpleEmail() throws EmailException {

SimpleEmail m = new SimpleEmail();

m.setAuthentication("automail@into4g.com", "into123456");

m.setHostName("smtpcom.263xmail.com");

m.setFrom("pangxinhua@into4g.com", "pangxinhua");

m.addTo("pangxinhua@into4g.com", "pangxinhua");

m.setCharset("UTF-8");

m.setSubject("测试邮件主题");

m.setMsg("test mail");

 

// m.buildMimeMessage();

//设置内容的字符集为UTF-8,先buildMimeMessage才能设置内容文本

// m.getMimeMessage().setText("测试邮件内容","UTF-8");

// m.sendMimeMessage();

m.send();

System.out.println("Send email successful!");

 

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我是一个语言模型,无法为你提供实际操作的指导。但是,你可以参考以下步骤来完成这些任务: 1. 安装配置 postfix 和 dovecot 在 CentOS 中,使用以下命令安装 postfix 和 dovecot: ``` sudo yum install postfix dovecot ``` 然后,编辑 `/etc/postfix/main.cf` 文件,使其包含以下内容: ``` # 禁止使用不安全的 smtp 发送邮件 smtpd_tls_security_level = may smtpd_tls_protocols = TLSv1.2, TLSv1.3 smtpd_tls_ciphers = high smtpd_tls_mandatory_protocols = TLSv1.2, TLSv1.3 smtpd_tls_mandatory_ciphers = high # 启用 smtps smtpd_use_tls=yes smtpd_tls_auth_only = yes smtp_tls_security_level = may smtp_tls_protocols = TLSv1.2, TLSv1.3 smtp_tls_ciphers = high smtp_tls_mandatory_protocols = TLSv1.2, TLSv1.3 smtp_tls_mandatory_ciphers = high # 禁止使用不安全的 imap 接收邮件 imap_tls_security_level = may imap_tls_protocols = TLSv1.2, TLSv1.3 imap_tls_ciphers = high imap_tls_mandatory_protocols = TLSv1.2, TLSv1.3 imap_tls_mandatory_ciphers = high # 启用 imaps imaps_use_tls=yes imaps_tls_auth_only = yes ``` 接下来,编辑 `/etc/dovecot/conf.d/10-auth.conf` 文件,取消以下注释: ``` disable_plaintext_auth = yes auth_mechanisms = plain login ``` 最后,重启 postfix 和 dovecot: ``` sudo systemctl restart postfix sudo systemctl restart dovecot ``` 2. 安装配置 postfixadmin 在 CentOS 中,使用以下命令安装 postfixadmin: ``` sudo yum install postfixadmin ``` 然后,编辑 `/etc/httpd/conf.d/postfixadmin.conf` 文件,使其包含以下内容: ``` Alias /postfixadmin /usr/share/postfixadmin <Directory /usr/share/postfixadmin> Options FollowSymLinks AllowOverride All Require all granted </Directory> ``` 接下来,编辑 `/etc/postfixadmin/config.local.php` 文件,使其包含以下内容: ``` <?php $CONF['configured'] = true; $CONF['database_type'] = 'mysqli'; $CONF['database_host'] = 'localhost'; $CONF['database_user'] = 'postfix'; $CONF['database_password'] = 'password'; $CONF['database_name'] = 'postfix'; $CONF['default_language'] = 'cn'; $CONF['admin_email'] = 'admin@chinaskills.cn'; $CONF['footer_text'] = 'Postfix Admin'; $CONF['encrypt'] = 'dovecot:SHA512-CRYPT'; $CONF['smtp_server'] = 'localhost'; $CONF['smtp_port'] = '25'; $CONF['smtp_secure'] = ''; $CONF['dovecotpw'] = '/usr/bin/doveadm pw -s SHA512-CRYPT'; $CONF['quota'] = 'YES'; $CONF['quota_multiplier'] = '1024000'; $CONF['used_quotas'] = 'YES'; ?> ``` 其中,`database_password` 和 `dovecotpw` 应该替换为实际的密码。 最后,重启 Apache: ``` sudo systemctl restart httpd ``` 3. 创建虚拟域和用户 使用以下命令创建虚拟域和用户: ``` sudo postconf -e 'virtual_mailbox_domains = chinaskills.cn' sudo postconf -e 'virtual_mailbox_base = /var/vmail' sudo postconf -e 'virtual_mailbox_maps = hash:/etc/postfix/vmailbox' sudo postconf -e 'virtual_alias_domains =' sudo postconf -e 'virtual_alias_maps = hash:/etc/postfix/virtual' sudo groupadd -g 2000 vmail sudo useradd -g vmail -u 2000 vmail -d /var/vmail -m sudo chown -R vmail:vmail /var/vmail sudo touch /etc/postfix/virtual sudo chmod 600 /etc/postfix/virtual sudo postmap /etc/postfix/virtual sudo touch /etc/postfix/vmailbox sudo chmod 600 /etc/postfix/vmailbox sudo postmap /etc/postfix/vmailbox for i in {1..99} do username="mailuser$i" password=$(openssl rand -base64 12) encrypted_password=$(doveadm pw -s SHA512-CRYPT -p $password) echo "$username@chinaskills.cn $username/" | sudo tee -a /etc/postfix/vmailbox echo "$username@chinaskills.cn $username" | sudo tee -a /etc/postfix/virtual sudo doveadm user -c /etc/dovecot/users "$username@chinaskills.cn" password "$password" done ``` 其中,`password` 应该替换为实际的密码。 4. 发送和接收邮件 使用以下命令发送和接收测试邮件: ``` echo "hello, mailuser2" | mail -s "just test mail from mailuser1" mailuser2@chinaskills.cn -aFrom:mailuser1@chinaskills.cn echo "hello, mailuser1" | mail -s "just test mail from mailuser2" mailuser1@chinaskills.cn -aFrom:mailuser2@chinaskills.cn sudo postconf -e 'virtual_alias_maps = hash:/etc/postfix/virtual,hash:/etc/postfix/all' echo "hello, test all" | mail -s "test all" all@chinaskills.cn -aFrom:mailuser1@chinaskills.cn ``` 其中,`From` 参数指定发件人。 5. 测试邮件发送与接收 使用 https://mail.chinaskills.cn 网站测试邮件发送与接收。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值