本文整理汇总了Java中com.sun.mail.smtp.SMTPMessage.setFrom方法的典型用法代码示例。如果您正苦于以下问题:Java SMTPMessage.setFrom方法的具体用法?Java SMTPMessage.setFrom怎么用?Java SMTPMessage.setFrom使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.mail.smtp.SMTPMessage
的用法示例。
在下文中一共展示了SMTPMessage.setFrom方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: composeMessage
import com.sun.mail.smtp.SMTPMessage; //导入方法依赖的package包/类
/**
* Helper method for composing a simple text MIME Message with the source.
* If from address is null, will use the internet address of the local host.
*
* @param source
* @return
* @throws SmtpMailException
*/
private SMTPMessage composeMessage(String source, Address from,
Address[] to, String subject) throws SmtpMailException {
SMTPMessage msg = new SMTPMessage(session);
try {
if (from == null) {
msg.setFrom();
} else {
msg.setFrom(from);
}
msg.setSubject(subject);
msg.setRecipients(Message.RecipientType.TO, to);
msg.setText(source);
msg.setSentDate(new Date());
return msg;
} catch (MessagingException e) {
throw new SmtpMailException("Error creating the message.", e);
}
}
示例2: testCaseSensitivity
import com.sun.mail.smtp.SMTPMessage; //导入方法依赖的package包/类
/**
* MNT-9289
*
* Change in case in email Subject causes DuplicateChildNodeNameException
*/
public void testCaseSensitivity() throws Exception
{
NodeRef person = personService.getPerson(TEST_USER);
String TEST_EMAIL="[email protected]";
NodeRef testUserHomeFolder = (NodeRef)nodeService.getProperty(person, ContentModel.PROP_HOMEFOLDER);
if(person == null)
{
logger.debug("new person created");
Map<QName, Serializable> props = new HashMap<QName, Serializable>();
props.put(ContentModel.PROP_USERNAME, TEST_USER);
props.put(ContentModel.PROP_EMAIL, TEST_EMAIL);
person = personService.createPerson(props);
}
nodeService.setProperty(person, ContentModel.PROP_EMAIL, TEST_EMAIL);
Set<String> auths = authorityService.getContainedAuthorities(null, "GROUP_EMAIL_CONTRIBUTORS", true);
if(!auths.contains(TEST_USER))
{
authorityService.addAuthority("GROUP_EMAIL_CONTRIBUTORS", TEST_USER);
}
String companyHomePathInStore = "/app:company_home";
String storePath = "workspace://SpacesStore";
StoreRef storeRef = new StoreRef(storePath);
NodeRef storeRootNodeRef = nodeService.getRootNode(storeRef);
List<NodeRef> nodeRefs = searchService.selectNodes(storeRootNodeRef, companyHomePathInStore, null, namespaceService, false);
NodeRef companyHomeNodeRef = nodeRefs.get(0);
assertNotNull("company home is null", companyHomeNodeRef);
String TEST_CASE_SENSITIVITY_SUBJECT = "Test (Mail)";
String testUserHomeDBID = ((Long)nodeService.getProperty(testUserHomeFolder, ContentModel.PROP_NODE_DBID)).toString() + "@Alfresco.com";
String from = TEST_EMAIL;
String to = testUserHomeDBID;
String content = "hello world";
Session sess = Session.getDefaultInstance(new Properties());
assertNotNull("sess is null", sess);
SMTPMessage msg = new SMTPMessage(sess);
InternetAddress[] toa = { new InternetAddress(to) };
EmailDelivery delivery = new EmailDelivery(to, from, null);
msg.setFrom(new InternetAddress(TEST_EMAIL));
msg.setRecipients(Message.RecipientType.TO, toa);
msg.setContent(content, "text/plain");
msg.setSubject(TEST_CASE_SENSITIVITY_SUBJECT);
ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
msg.writeTo(bos1);
InputStream is = IOUtils.toInputStream(bos1.toString());
assertNotNull("is is null", is);
SubethaEmailMessage m = new SubethaEmailMessage(is);
folderEmailMessageHandler.setOverwriteDuplicates(false);
emailService.importMessage(delivery, m);
QName safeQName = QName.createQNameWithValidLocalName(NamespaceService.CONTENT_MODEL_1_0_URI, TEST_CASE_SENSITIVITY_SUBJECT);
List<ChildAssociationRef> assocs = nodeService.getChildAssocs(testUserHomeFolder, ContentModel.ASSOC_CONTAINS, safeQName);
assertEquals(1, assocs.size());
msg.setSubject(TEST_CASE_SENSITIVITY_SUBJECT.toUpperCase());
ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
msg.writeTo(bos2);
is = IOUtils.toInputStream(bos2.toString());
assertNotNull("is is null", is);
m = new SubethaEmailMessage(is);
folderEmailMessageHandler.setOverwriteDuplicates(false);
emailService.importMessage(delivery, m);
safeQName = QName.createQNameWithValidLocalName(NamespaceService.CONTENT_MODEL_1_0_URI, TEST_CASE_SENSITIVITY_SUBJECT.toUpperCase() + "(1)");
assocs = nodeService.getChildAssocs(testUserHomeFolder, ContentModel.ASSOC_CONTAINS, safeQName);
assertEquals(1, assocs.size());
}
示例3: sendEmail
import com.sun.mail.smtp.SMTPMessage; //导入方法依赖的package包/类
public void sendEmail(String subject, String commaSeparatedRecipients, String body, List<File> attachments) {
Session session = Session.getDefaultInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(properties.getProperty("mail.smtp.user"), properties.getProperty("mail.smtp.password"));
}
});
try {
SMTPMessage message = new SMTPMessage(session);
message.setFrom(new InternetAddress(properties.getProperty("mail.sender")));
if (commaSeparatedRecipients == null || commaSeparatedRecipients.isEmpty())
commaSeparatedRecipients = properties.getProperty("author.email");
message.addRecipients(Message.RecipientType.TO, getAddresses(commaSeparatedRecipients));
message.setSubject(subject);
// message.setHeader("Content-Type", "text/html; charset=UTF-8");
// message.setText( body, "UTF-8", "html" );
// message.setContent(body, "text/html");
message.saveChanges();
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setHeader("Content-Type", "text/html; charset=UTF-8");
// messageBodyPart.setContent(body, "text/html");
messageBodyPart.setText(body, "UTF-8", "html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
for (File file : attachments) {
if (file == null || !file.exists())
continue;
messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(file)));
messageBodyPart.setFileName(file.getName());
multipart.addBodyPart(messageBodyPart);
}
// Put parts in message
message.setContent(multipart);
message.setSendPartial(true);
// Transport transport = session.getTransport("smtps");
// transport.connect(properties.getProperty("mail.smtp.host"), properties.getProperty("mail.smtp.user"), properties.getProperty("mail.smtp.password"));
// transport.sendMessage(message, message.getAllRecipients());
// transport.close();
Transport.send(message);
System.out.println("Sent Expense successfully....");
} catch (MessagingException mex) {
throw new RuntimeException(mex);
}
}