本文整理汇总了Java中com.sun.mail.smtp.SMTPMessage类的典型用法代码示例。如果您正苦于以下问题:Java SMTPMessage类的具体用法?Java SMTPMessage怎么用?Java SMTPMessage使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SMTPMessage类属于com.sun.mail.smtp包,在下文中一共展示了SMTPMessage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: addHeadersToSMTPMessage
import com.sun.mail.smtp.SMTPMessage; //导入依赖的package包/类
/**
* The method creates a copy of the SMTPMessage object passed through the arguments list and adds the headers to the
* copied object then returns it. If the header is already present in the message then its values list will be
* updated with the given header value.
* @param message The SMTPMessage object to which the headers are added or updated.
* @param headerNames A list of strings containing the header names that need to be added or updated.
* @param headerValues A list of strings containing the header values that need to be added.
* @return The method returns the message with the headers added.
* @throws MessagingException
*/
protected SMTPMessage addHeadersToSMTPMessage(SMTPMessage message, List<String> headerNames,
List<String> headerValues) throws MessagingException {
SMTPMessage msg = new SMTPMessage(message);
Iterator namesIter = headerNames.iterator();
Iterator valuesIter = headerValues.iterator();
while (namesIter.hasNext() && valuesIter.hasNext()) {
String headerName = (String) namesIter.next();
String headerValue = (String) valuesIter.next();
if (msg.getHeader(headerName) != null) {
// then a header with this name already exists, add the headerValue to the existing values list.
msg.addHeader(headerName, headerValue);
} else {
msg.setHeader(headerName, headerValue);
}
}
return msg;
}
示例3: setUp
import com.sun.mail.smtp.SMTPMessage; //导入依赖的package包/类
/**
* Initialize tested object and set up the mocks.
*
* @throws Exception
*/
@Before
public void setUp() throws Exception {
sendMail = new SendMail();
inputs = new SendMailInputs();
PowerMockito.whenNew(Properties.class).withNoArguments().thenReturn(propertiesMock);
Mockito.doReturn(SMTP_HOSTANME).when(propertiesMock).put(SMTP_HOST_CONFIG, SMTP_HOSTANME);
PowerMockito.mockStatic(Session.class);
PowerMockito.doReturn(sessionMock).when(Session.class, "getInstance", anyObject(),
anyObject());
PowerMockito.whenNew(SMTPMessage.class).withArguments(sessionMock).thenReturn(smtpMessageMock);
PowerMockito.whenNew(MimeMultipart.class).withNoArguments().thenReturn(mimeMultipartMock);
PowerMockito.whenNew(MimeBodyPart.class).withNoArguments().thenReturn(mimeBodyPartMock);
Mockito.doNothing().when(mimeBodyPartMock).setHeader(anyString(), anyString());
Mockito.doNothing().when(mimeMultipartMock).addBodyPart(mimeBodyPartMock);
Mockito.doNothing().when(smtpMessageMock).setContent(mimeMultipartMock);
Mockito.doNothing().when(smtpMessageMock).setFrom(Matchers.<InternetAddress>any());
Mockito.doNothing().when(smtpMessageMock).setSubject(anyString());
PowerMockito.whenNew(InternetAddress.class).withArguments(anyString()).thenReturn(recipientMock);
Mockito.doNothing()
.when(smtpMessageMock)
.setRecipients(Matchers.<Message.RecipientType>any(), Matchers.<InternetAddress[]>any());
}
示例4: testExecuteWithHeaders
import com.sun.mail.smtp.SMTPMessage; //导入依赖的package包/类
/**
* Test if the method addHeadersToSMTPMessage is called if headers are supplied to be added to the mail.
* @throws Exception
*/
@Test
public void testExecuteWithHeaders() throws Exception {
Mockito.doReturn(transportMock).when(sessionMock).getTransport(SMTP_PROTOCOL);
inputs.setSmtpHostname(SMTP_HOSTANME);
inputs.setPort(PORT);
inputs.setFrom(FROM);
inputs.setTo(TO);
inputs.setCc(CC);
inputs.setBcc(BCC);
inputs.setSubject(SUBJECT);
inputs.setBody(BODY);
inputs.setUser(USER);
inputs.setPassword(PASSWORD);
inputs.setHeaders(HEADERS_WITH_DEFAULT_DELIMIETRS);
doReturn(msgMock).when(sendMailSpy).addHeadersToSMTPMessage(Matchers.<SMTPMessage>any(), anyList(), anyList());
sendMailSpy.execute(inputs);
verify(sendMailSpy).addHeadersToSMTPMessage(Matchers.<SMTPMessage>any(), anyList(), anyList());
}
示例5: testAddHeadersToSMTPMessage
import com.sun.mail.smtp.SMTPMessage; //导入依赖的package包/类
/**
* Test addHeadersToSMTPMessage method when adding 3 headers and two of them have the same name but different values,
* so the method should add the second value to the same header.
* @throws MessagingException
*/
@Test
public void testAddHeadersToSMTPMessage() throws MessagingException {
ArrayList<String> headerNames = new ArrayList<>();
headerNames.add(0, "Sensitivity");
headerNames.add(1, "message-type");
headerNames.add(2, "Sensitivity");
ArrayList<String> headerValues = new ArrayList<>();
headerValues.add(0, "Company-Confidential");
headerValues.add(1, "Multiple Part");
headerValues.add(2, "Personal");
doReturn(null).doReturn(new String[] {"Company-Confidential"}).when(msgMock).getHeader("Sensitivity");
Mockito.when(new SMTPMessage(smtpMessageMock)).thenReturn(msgMock);
sendMail.addHeadersToSMTPMessage(smtpMessageMock, headerNames, headerValues);
verify(msgMock, times(1)).addHeader(Matchers.anyString(), Matchers.anyString());
verify(msgMock, times(2)).setHeader(Matchers.anyString(), Matchers.anyString());
verify(msgMock, times(3)).getHeader(Matchers.anyString());
}
示例6: 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());
}
示例7: setRecipients
import com.sun.mail.smtp.SMTPMessage; //导入依赖的package包/类
/**
* Sets recipients.
*
* <b>NOTE: If {@link IMailBL#getDebugMailToAddressOrNull(Properties)} returns a valid mail address, it will send to that instead!</b>
*
* @param message
* @param type
* @param addresses
* @throws MessagingException
*/
private void setRecipients(final SMTPMessage message, final RecipientType type, final List<? extends Address> addresses) throws MessagingException
{
if (addresses == null || addresses.isEmpty())
{
return;
}
final InternetAddress debugMailTo = Services.get(IMailBL.class).getDebugMailToAddressOrNull(Env.getCtx());
if (debugMailTo != null)
{
if (Message.RecipientType.TO.equals(type))
{
message.setRecipient(Message.RecipientType.TO, debugMailTo);
}
for (final Address address : addresses)
{
message.addHeader("X-metasfreshDebug-Original-Address" + type.toString(), address.toString());
}
}
else
{
final Address[] addressesArr = addresses.toArray(new Address[addresses.size()]);
message.setRecipients(Message.RecipientType.TO, addressesArr);
}
}
示例8: setHeaders
import com.sun.mail.smtp.SMTPMessage; //导入依赖的package包/类
private static void setHeaders(SMTPMessage msg, Map<String,String> headers) throws MessagingException {
Iterator<Entry<String, String>> it = headers.entrySet().iterator();
Entry<String, String> e;
while(it.hasNext()) {
e = it.next();
msg.setHeader(e.getKey(),e.getValue());
}
}
示例9: testAddHeadersToSMTPMessageThrowsException
import com.sun.mail.smtp.SMTPMessage; //导入依赖的package包/类
/**
* Test addHeadersToSMTPMessage method when it throws a MessagingException.
* @throws MessagingException
*/
@Test
public void testAddHeadersToSMTPMessageThrowsException() throws MessagingException {
ArrayList<String> headerNames = new ArrayList<>();
headerNames.add(0, "Sensitivity");
ArrayList<String> headerValues = new ArrayList<>();
headerValues.add(0, "Company-Confidential");
Mockito.when(new SMTPMessage(smtpMessageMock)).thenReturn(msgMock);
doThrow(new MessagingException()).when(msgMock).getHeader(Matchers.anyString());
exception.expect(MessagingException.class);
sendMail.addHeadersToSMTPMessage(smtpMessageMock, headerNames, headerValues);
}
示例10: testExecuteGoesToSuccessScenario1
import com.sun.mail.smtp.SMTPMessage; //导入依赖的package包/类
/**
* Test Execute method with successful scenario,
* (user not null, html input takes the default value(false)).
*
* @throws Exception
*/
@Test
public void testExecuteGoesToSuccessScenario1() throws Exception {
Mockito.doReturn(transportMock).when(sessionMock).getTransport(SMTP_PROTOCOL);
Mockito.doNothing().when(transportMock).connect(SMTP_HOSTANME, INT_PORT, USER, PASSWORD);
Mockito.doNothing().when(transportMock).sendMessage(Matchers.<SMTPMessage>any(), Matchers.<Address[]>any());
Mockito.doReturn(addresses).when(smtpMessageMock).getAllRecipients();
Mockito.doNothing().when(mimeBodyPartMock).setContent(BODY, TEXT_PLAIN + CHARSET_CST + DEFAULT_CHARACTERSET);
inputs.setSmtpHostname(SMTP_HOSTANME);
inputs.setPort(PORT);
inputs.setFrom(FROM);
inputs.setTo(TO);
inputs.setCc(CC);
inputs.setBcc(BCC);
inputs.setSubject(SUBJECT);
inputs.setBody(BODY);
inputs.setUser(USER);
inputs.setPassword(PASSWORD);
Map<String, String> result = sendMail.execute(inputs);
assertEquals(MAIL_WAS_SENT, result.get(RETURN_RESULT));
assertEquals(SUCCESS_RETURN_CODE, result.get(RETURN_CODE));
verifyCommonMethodInvocations();
verify(propertiesMock).put(eq(SMTP_USER_CONFIG), eq(USER));
verify(propertiesMock).put(eq(SMTP_PASSWORD_CONFIG), eq(PASSWORD));
verify(propertiesMock).put(eq(SMTP_AUTH_CONFIG), eq("true"));
verify(sessionMock).getTransport(SMTP_PROTOCOL);
verify(transportMock).connect(SMTP_HOSTANME, INT_PORT, USER, PASSWORD);
verify(transportMock).sendMessage(Matchers.<SMTPMessage>any(), Matchers.<Address[]>any());
verify(mimeBodyPartMock).setContent(BODY, TEXT_PLAIN + CHARSET_CST + DEFAULT_CHARACTERSET);
verify(transportMock).close();
}
示例11: testExecuteGoesToSuccessScenario2
import com.sun.mail.smtp.SMTPMessage; //导入依赖的package包/类
/**
* Test Execute method with successful scenario,
* (user is null, html inputs is true).
*
* @throws Exception
*/
@Test
public void testExecuteGoesToSuccessScenario2() throws Exception {
prepareTransportClassForStaticMock();
Mockito.doNothing().when(mimeBodyPartMock).setContent(BODY, TEXT_HTML + CHARSET_CST + DEFAULT_CHARACTERSET);
inputs.setSmtpHostname(SMTP_HOSTANME);
inputs.setPort(PORT);
inputs.setFrom(FROM);
inputs.setTo(TO);
inputs.setCc(CC);
inputs.setBcc(BCC);
inputs.setSubject(SUBJECT);
inputs.setBody(BODY);
inputs.setHtmlEmail(HTML_EMAIL_TRUE);
Map<String, String> result = sendMail.execute(inputs);
assertEquals(MAIL_WAS_SENT, result.get(RETURN_RESULT));
assertEquals(SUCCESS_RETURN_CODE, result.get(RETURN_CODE));
verifyCommonMethodInvocations();
verify(propertiesMock, never()).put(eq(SMTP_USER_CONFIG), eq(USER));
verify(propertiesMock, never()).put(eq(SMTP_PASSWORD_CONFIG), eq(PASSWORD));
verify(propertiesMock, never()).put(eq(SMTP_AUTH_CONFIG), eq("true"));
verify(sessionMock, never()).getTransport(SMTP_PROTOCOL);
verify(transportMock, never()).connect(SMTP_HOSTANME, INT_PORT, USER, PASSWORD);
verify(transportMock, never()).sendMessage(Matchers.<SMTPMessage>any(), Matchers.<Address[]>any());
PowerMockito.verifyStatic(Mockito.times(1));
Transport.send(smtpMessageMock);
verify(mimeBodyPartMock).setContent(BODY, TEXT_HTML + CHARSET_CST + DEFAULT_CHARACTERSET);
}
示例12: verifyCommons
import com.sun.mail.smtp.SMTPMessage; //导入依赖的package包/类
private void verifyCommons() throws Exception {
verify(propertiesMock).put(eq(SMTP_HOST_CONFIG), eq(SMTP_HOSTANME));
verify(propertiesMock).put(eq(SMTP_PORT_CONFIG), eq(PORT));
PowerMockito.verifyStatic(Mockito.times(1));
Session.getInstance(propertiesMock, null);
verify(mimeBodyPartMock).setHeader(CONTENT_TRANSFER_ENCODING, DEFAULT_CONTENT_TRANSFER_ENCODING);
verify(smtpMessageMock).setContent(mimeMultipartMock);
verify(smtpMessageMock).setFrom(Matchers.<InternetAddress>any());
verify(smtpMessageMock).setSubject(anyString());
PowerMockito.verifyNew(Properties.class).withNoArguments();
PowerMockito.verifyNew(SMTPMessage.class).withArguments(sessionMock);
PowerMockito.verifyNew(MimeMultipart.class).withNoArguments();
PowerMockito.verifyNew(InternetAddress.class, atLeastOnce()).withArguments(anyString());
}
示例13: setSmtpMessage
import com.sun.mail.smtp.SMTPMessage; //导入依赖的package包/类
public void setSmtpMessage(SMTPMessage newSmtpMsg) {
g_smtpMsg = newSmtpMsg;
}
示例14: getSmtpMessage
import com.sun.mail.smtp.SMTPMessage; //导入依赖的package包/类
public SMTPMessage getSmtpMessage() {
return g_smtpMsg;
}
示例15: testExecuteGoesToSuccessScenarioWithHTMLAndBase64Images
import com.sun.mail.smtp.SMTPMessage; //导入依赖的package包/类
@Test
public void testExecuteGoesToSuccessScenarioWithHTMLAndBase64Images() throws Exception {
prepareTransportClassForStaticMock();
Mockito.doNothing().when(mimeBodyPartMock).setContent(HTML_BODY_BASE64_IMAGES_AFTER_PROCESSING,
TEXT_HTML + CHARSET_CST + DEFAULT_CHARACTERSET);
inputs.setSmtpHostname(SMTP_HOSTANME);
inputs.setPort(PORT);
inputs.setFrom(FROM);
inputs.setTo(TO);
inputs.setCc(CC);
inputs.setBcc(BCC);
inputs.setSubject(SUBJECT);
inputs.setBody(HTML_BODY_BASE64_IMAGES);
inputs.setHtmlEmail(HTML_EMAIL_TRUE);
Map<String, String> result = sendMail.execute(inputs);
assertEquals(MAIL_WAS_SENT, result.get(RETURN_RESULT));
assertEquals(SUCCESS_RETURN_CODE, result.get(RETURN_CODE));
PowerMockito.verifyNew(MimeBodyPart.class, Mockito.times(2)).withNoArguments();
verify(mimeMultipartMock, Mockito.times(2)).addBodyPart(mimeBodyPartMock);
verifyCommons();
verify(propertiesMock, never()).put(eq(SMTP_USER_CONFIG), eq(USER));
verify(propertiesMock, never()).put(eq(SMTP_PASSWORD_CONFIG), eq(PASSWORD));
verify(propertiesMock, never()).put(eq(SMTP_AUTH_CONFIG), eq("true"));
verify(sessionMock, never()).getTransport(SMTP_PROTOCOL);
verify(transportMock, never()).connect(SMTP_HOSTANME, INT_PORT, USER, PASSWORD);
verify(transportMock, never()).sendMessage(Matchers.<SMTPMessage>any(), Matchers.<Address[]>any());
PowerMockito.verifyStatic(Mockito.times(1));
Transport.send(smtpMessageMock);
verify(mimeBodyPartMock).setContent(argThat(new ArgumentMatcher<String>() {
@Override
public boolean matches(Object o) {
if (o instanceof String) {
String body = (String) o;
int indexOfCID = body.indexOf("cid:") + 4;
String contentId = body.substring(indexOfCID, (body.indexOf("\">", indexOfCID)));
String partBeforeContentId = HTML_BODY_BASE64_IMAGES_AFTER_PROCESSING.substring(0,
HTML_BODY_BASE64_IMAGES_AFTER_PROCESSING.indexOf("cid:") + 3);
String partAfterContentID = HTML_BODY_BASE64_IMAGES_AFTER_PROCESSING.substring(
HTML_BODY_BASE64_IMAGES_AFTER_PROCESSING.indexOf("cid:") + 23,
HTML_BODY_BASE64_IMAGES_AFTER_PROCESSING.length());
if (body.contains(partBeforeContentId) && body.contains(partAfterContentID) &&
contentId.matches("\\d{1,5}\\.\\d{12,13}")) {
return true;
}
}
return false;
}
}), eq(TEXT_HTML + CHARSET_CST + DEFAULT_CHARACTERSET));
}