James收发邮件

下载James

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

启动James

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

James收发邮件_第1张图片

双击bin目录下的run.bat

James收发邮件_第2张图片

已启动

修改配置文件

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

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

修改

Postmaster@localhost

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

修改

localhost

localhost改为chen.cn

修改

true都改为false

找到:


 relay-denied 
550 - Requested action not taken: relaying denied

把它注释掉

创建用户

用telnet连接到james的管理界面

telnet localhsot 4555

idpassword都是root

James收发邮件_第3张图片

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 = "[email protected]";
		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("[email protected]"),
				new InternetAddress("[email protected]")
		});
		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();
	}
}


你可能感兴趣的:(java,mail)