James收发邮件

下载James

apache的官网,找到James,下载。这里用的是2.3.2版本。

启动James

将下载好的James解压,如下图:


双击bin目录下的run.bat


已启动

修改配置文件

打开\apps\james\SAR-INF\config.xml

建议先大致读一遍这个配置文件,大概了解一下都配置了什么东东。

修改

<postmaster>Postmaster@localhost</postmaster>

localhost改为所需的域名如:chen.cn

修改

<servername>localhost</servername>

localhost改为chen.cn

修改

<servernames autodetect="true" autodetectIP="true">

true都改为false

找到:

<mailet match="RemoteAddrNotInNetwork=127.0.0.1" class="ToProcessor">
<processor> relay-denied </processor>
<notice>550 - Requested action not taken: relaying denied</notice>
</mailet>

把它注释掉

创建用户

用telnet连接到james的管理界面

telnet localhsot 4555

idpassword都是root


help就知道怎么添加用户了,这里添加两个用户:

adduser chen chen

adduser uchen uchen

javaMail收发邮件


代码:

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JamesJavaMail
{
	public static void sendMail() throws Exception
	{
		String host = "localhost";
		final String from = "chen@chen.cn";
		final String password = "chen";

		Properties properties = new Properties();
		properties.setProperty("mail.smtp.host", host);
		properties.setProperty("mail.smtp.auth", "true");
		properties.setProperty("mail.transport.protocol", "smtp");

		Session session = Session.getDefaultInstance(properties,
				new Authenticator()
				{
					@Override
					public PasswordAuthentication getPasswordAuthentication()
					{
						return new PasswordAuthentication(from, password);
					}
				});

		session.setDebug(true);

		Message msg = new MimeMessage(session);
		msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(from));

		msg.setSubject("test");
		msg.setText("I'm from "
				+ from
				+ ",just test java mail!"
				+ new SimpleDateFormat("yyyy-MM-dd- HH:mm:ss")
						.format(new Date()));

		Transport transport = session.getTransport();

		// 端口为25
		transport.connect(host, 25, from, "chen");
		transport.sendMessage(msg, new Address[]
		{
				new InternetAddress("623799957@qq.com"),
				new InternetAddress("uchen@chen.cn")
		});
		transport.close();
		System.out.println("发好了!");
	}

	public static void receiveMail() throws Exception
	{
		String host = "localhost";
		final String username = "uchen";
		final String password = "uchen";

		Properties properties = new Properties();
		properties.setProperty("mail.pop3.host", "localhost");
		properties.setProperty("mail.pop3.auth", "true");
		properties.setProperty("mail.transport.protocol", "pop3");

		Session session = Session.getDefaultInstance(properties,
				new Authenticator()
				{
					@Override
					public PasswordAuthentication getPasswordAuthentication()
					{
						return new PasswordAuthentication(username, password);
					}
				});

		try
		{
			Store store = session.getStore("pop3");
			store.connect(host, username, password);

			Folder folder = store.getFolder("INBOX");
			folder.open(Folder.READ_ONLY); // 打开

			Message message[] = folder.getMessages();
			int n = message.length;
			System.out.println("一共有 " + n + " 封");
			if (n > 0)
			{
				for (Message m : message)
				{
					System.out.println(m.getFrom()[0] + "-----------"
							+ m.getSubject());
					try
					{
						m.writeTo(System.out);
					}
					catch (IOException e)
					{
						e.printStackTrace();
					}
				}
			}
			folder.close(false);
			store.close();

		}
		catch (MessagingException e)
		{
			e.printStackTrace();
		}
		System.out.println("收好了!!");
	}

	public static void main(String[] args) throws Exception
	{
		sendMail();
		// receiveMail();
	}
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值