Java by API/javax.mail/Message
Содержание
- 1 Message: getDisposition()
- 2 Message: getFrom()
- 3 Message: getLineCount()
- 4 Message: getReplyTo()
- 5 Message: getSize()
- 6 Message.RecipientType.BCC
- 7 Message.RecipientType.CC
- 8 Message.RecipientType.TO
- 9 Message: setFrom(Address address)
- 10 Message: setRecipient(Message.RecipientType type, Address address)
- 11 Message: setSubject(String subject)
Message: getDisposition()
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.URLName;
public class Main {
public static void main(String[] args) throws Exception {
URLName server = new URLName("protocol://username@host/foldername");
Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator());
Folder folder = session.getFolder(server);
if (folder == null) {
System.out.println("Folder " + server.getFile() + " not found.");
System.exit(1);
}
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
System.out.println(messages[i].getSize() + " bytes long.");
System.out.println(messages[i].getLineCount() + " lines.");
String disposition = messages[i].getDisposition();
if (disposition == null){
; // do nothing
}else if (disposition.equals(Part.INLINE)) {
System.out.println("This part should be displayed inline");
} else if (disposition.equals(Part.ATTACHMENT)) {
System.out.println("This part is an attachment");
String fileName = messages[i].getFileName();
System.out.println("The file name of this attachment is " + fileName);
}
String description = messages[i].getDescription();
if (description != null) {
System.out.println("The description of this message is " + description);
}
}
folder.close(false);
}
}
class MailAuthenticator extends Authenticator {
public MailAuthenticator() {
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
}
Message: getFrom()
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
public class Main {
public static void main(String[] args) throws Exception {
URLName server = new URLName("protocol://username@host/foldername");
Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator());
Folder folder = session.getFolder(server);
if (folder == null) {
System.out.println("Folder " + server.getFile() + " not found.");
System.exit(1);
}
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
System.out.println("Message " + (i + 1));
System.out.println("From: " + InternetAddress.toString(messages[i].getFrom()));
System.out.println("Reply-to: " + InternetAddress.toString(messages[i].getReplyTo()));
String to = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.TO));
System.out.println("To: " + to);
String cc = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.CC));
System.out.println("Cc: " + cc);
String bcc = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.BCC));
System.out.println("Bcc: " + bcc);
System.out.println("Subject: " + messages[i].getSubject());
System.out.println("Sent: " + messages[i].getSentDate());
System.out.println("Received: " + messages[i].getReceivedDate());
}
folder.close(false);
}
}
class MailAuthenticator extends Authenticator {
public MailAuthenticator() {
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
}
Message: getLineCount()
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.URLName;
public class Main {
public static void main(String[] args) throws Exception {
URLName server = new URLName("protocol://username@host/foldername");
Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator());
Folder folder = session.getFolder(server);
if (folder == null) {
System.out.println("Folder " + server.getFile() + " not found.");
System.exit(1);
}
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
System.out.println(messages[i].getSize() + " bytes long.");
System.out.println(messages[i].getLineCount() + " lines.");
String disposition = messages[i].getDisposition();
if (disposition == null){
; // do nothing
}else if (disposition.equals(Part.INLINE)) {
System.out.println("This part should be displayed inline");
} else if (disposition.equals(Part.ATTACHMENT)) {
System.out.println("This part is an attachment");
String fileName = messages[i].getFileName();
System.out.println("The file name of this attachment is " + fileName);
}
String description = messages[i].getDescription();
if (description != null) {
System.out.println("The description of this message is " + description);
}
}
folder.close(false);
}
}
class MailAuthenticator extends Authenticator {
public MailAuthenticator() {
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
}
Message: getReplyTo()
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
public class Main {
public static void main(String[] args) throws Exception {
URLName server = new URLName("protocol://username@host/foldername");
Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator());
Folder folder = session.getFolder(server);
if (folder == null) {
System.out.println("Folder " + server.getFile() + " not found.");
System.exit(1);
}
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
System.out.println("Message " + (i + 1));
System.out.println("From: " + InternetAddress.toString(messages[i].getFrom()));
System.out.println("Reply-to: " + InternetAddress.toString(messages[i].getReplyTo()));
String to = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.TO));
System.out.println("To: " + to);
String cc = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.CC));
System.out.println("Cc: " + cc);
String bcc = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.BCC));
System.out.println("Bcc: " + bcc);
System.out.println("Subject: " + messages[i].getSubject());
System.out.println("Sent: " + messages[i].getSentDate());
System.out.println("Received: " + messages[i].getReceivedDate());
}
folder.close(false);
}
}
class MailAuthenticator extends Authenticator {
public MailAuthenticator() {
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
}
Message: getSize()
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.URLName;
public class Main {
public static void main(String[] args) throws Exception {
URLName server = new URLName("protocol://username@host/foldername");
Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator());
Folder folder = session.getFolder(server);
if (folder == null) {
System.out.println("Folder " + server.getFile() + " not found.");
System.exit(1);
}
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
System.out.println(messages[i].getSize() + " bytes long.");
System.out.println(messages[i].getLineCount() + " lines.");
String disposition = messages[i].getDisposition();
if (disposition == null){
; // do nothing
}else if (disposition.equals(Part.INLINE)) {
System.out.println("This part should be displayed inline");
} else if (disposition.equals(Part.ATTACHMENT)) {
System.out.println("This part is an attachment");
String fileName = messages[i].getFileName();
System.out.println("The file name of this attachment is " + fileName);
}
String description = messages[i].getDescription();
if (description != null) {
System.out.println("The description of this message is " + description);
}
}
folder.close(false);
}
}
class MailAuthenticator extends Authenticator {
public MailAuthenticator() {
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
}
Message.RecipientType.BCC
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
public class Main {
public static void main(String[] args) throws Exception {
URLName server = new URLName("protocol://username@host/foldername");
Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator());
Folder folder = session.getFolder(server);
if (folder == null) {
System.out.println("Folder " + server.getFile() + " not found.");
System.exit(1);
}
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
System.out.println("Message " + (i + 1));
System.out.println("From: " + InternetAddress.toString(messages[i].getFrom()));
System.out.println("Reply-to: " + InternetAddress.toString(messages[i].getReplyTo()));
String to = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.TO));
System.out.println("To: " + to);
String cc = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.CC));
System.out.println("Cc: " + cc);
String bcc = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.BCC));
System.out.println("Bcc: " + bcc);
System.out.println("Subject: " + messages[i].getSubject());
System.out.println("Sent: " + messages[i].getSentDate());
System.out.println("Received: " + messages[i].getReceivedDate());
}
folder.close(false);
}
}
class MailAuthenticator extends Authenticator {
public MailAuthenticator() {
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
}
Message.RecipientType.CC
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
public class Main {
public static void main(String[] args) throws Exception {
URLName server = new URLName("protocol://username@host/foldername");
Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator());
Folder folder = session.getFolder(server);
if (folder == null) {
System.out.println("Folder " + server.getFile() + " not found.");
System.exit(1);
}
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
System.out.println("Message " + (i + 1));
System.out.println("From: " + InternetAddress.toString(messages[i].getFrom()));
System.out.println("Reply-to: " + InternetAddress.toString(messages[i].getReplyTo()));
String to = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.TO));
System.out.println("To: " + to);
String cc = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.CC));
System.out.println("Cc: " + cc);
String bcc = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.BCC));
System.out.println("Bcc: " + bcc);
System.out.println("Subject: " + messages[i].getSubject());
System.out.println("Sent: " + messages[i].getSentDate());
System.out.println("Received: " + messages[i].getReceivedDate());
}
folder.close(false);
}
}
class MailAuthenticator extends Authenticator {
public MailAuthenticator() {
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
}
Message.RecipientType.TO
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
public class Main {
public static void main(String[] args) throws Exception {
URLName server = new URLName("protocol://username@host/foldername");
Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator());
Folder folder = session.getFolder(server);
if (folder == null) {
System.out.println("Folder " + server.getFile() + " not found.");
System.exit(1);
}
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
System.out.println("Message " + (i + 1));
System.out.println("From: " + InternetAddress.toString(messages[i].getFrom()));
System.out.println("Reply-to: " + InternetAddress.toString(messages[i].getReplyTo()));
String to = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.TO));
System.out.println("To: " + to);
String cc = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.CC));
System.out.println("Cc: " + cc);
String bcc = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.BCC));
System.out.println("Bcc: " + bcc);
System.out.println("Subject: " + messages[i].getSubject());
System.out.println("Sent: " + messages[i].getSentDate());
System.out.println("Received: " + messages[i].getReceivedDate());
}
folder.close(false);
}
}
class MailAuthenticator extends Authenticator {
public MailAuthenticator() {
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
}
Message: setFrom(Address address)
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Main {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put("mail.host", "mail.cloud9.net");
Session mailConnection = Session.getInstance(props, null);
Message msg = new MimeMessage(mailConnection);
Address a = new InternetAddress("a@a.ru", "A a");
Address b = new InternetAddress("fake@jexp.ru");
msg.setContent("Mail contect", "text/plain");
msg.setFrom(a);
msg.setRecipient(Message.RecipientType.TO, b);
msg.setSubject("subject");
Transport.send(msg);
}
}
Message: setRecipient(Message.RecipientType type, Address address)
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Main {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put("mail.host", "mail.cloud9.net");
Session mailConnection = Session.getInstance(props, null);
Message msg = new MimeMessage(mailConnection);
Address a = new InternetAddress("a@a.ru", "A a");
Address b = new InternetAddress("fake@jexp.ru");
msg.setContent("Mail contect", "text/plain");
msg.setFrom(a);
msg.setRecipient(Message.RecipientType.TO, b);
msg.setSubject("subject");
Transport.send(msg);
}
}
Message: setSubject(String subject)
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Main {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put("mail.host", "mail.cloud9.net");
Session mailConnection = Session.getInstance(props, null);
Message msg = new MimeMessage(mailConnection);
Address a = new InternetAddress("a@a.ru", "A a");
Address b = new InternetAddress("fake@jexp.ru");
msg.setContent("Mail contect", "text/plain");
msg.setFrom(a);
msg.setRecipient(Message.RecipientType.TO, b);
msg.setSubject("subject");
Transport.send(msg);
}
}