|
03.08.2012, 18:39:58
#37903936
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
Ссылка на профиль пользователя:
|
|
|
Участник
Откуда: Украина
Сообщения: 360
Рейтинг:
0
/ 0
|
|
|
|
Коллеги может кто то решал такой вопрос
аттачи не прикрепляются !
Взял исходники с http://www.akadia.com/services/java_mail_plsql.html
чуть изменил
Help !
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169.
create or replace and compile java source named "SendMail" as
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeUtility;
import javax.mail.BodyPart;
import javax.mail.Authenticator;
import javax.mail.Session;
import javax.mail.Store;
import javax.activation.*;
import java.security.Security;
public class SendMail {
// Sender, Recipient, CCRecipient, and BccRecipient are
// commaseparated lists of addresses.
// Body can span multiple CR/LF-separated lines.
// Attachments is a ///-separated list of file names.
public static int Send(String SMTPServer,
String Sender,
String Recipient,
final String Username,
final String Password,
String CcRecipient,
String BccRecipient,
String Subject,
String Body,
String ErrorMessage[],
String Attachments) {
// Error status
int ErrorStatus = 0;
// Create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", SMTPServer);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.debug", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Username, Password);
}
});
session.setDebug(true);
try {
// Create a message
MimeMessage msg = new MimeMessage(session);
// extracts the senders and adds them to the message
// Sender is a comma-separated list of e-mail addresses as per RFC822
{
InternetAddress[] TheAddresses = InternetAddress.parse(Sender);
msg.addFrom(TheAddresses);
}
// Extract the recipients and assign them to the message.
// Recipient is a comma-separated list of e-mail addresses as per RFC822.
{
InternetAddress[] TheAddresses = InternetAddress.parse(Recipient);
msg.addRecipients(Message.RecipientType.TO,TheAddresses);
}
// Extract the Cc-recipients and assign them to the message.
// CcRecipient is a comma-separated list of e-mail addresses as per RFC822.
if (null != CcRecipient) {
InternetAddress[] TheAddresses = InternetAddress.parse(CcRecipient);
msg.addRecipients(Message.RecipientType.CC,TheAddresses);
}
// Extract the Bcc-recipients and assign them to the message.
// BccRecipient is a comma-separated list of e-mail addresses as per RFC822.
if (null != BccRecipient) {
InternetAddress[] TheAddresses = InternetAddress.parse(BccRecipient);
msg.addRecipients(Message.RecipientType.BCC,TheAddresses);
}
// Subject field
msg.setSubject(Subject);
// Create the Multipart to be added the parts to
Multipart mp = new MimeMultipart();
// Create and fill the first message part
/* if (null != Body)
{
BodyPart mbp = new MimeBodyPart();
mbp.setContent( Body, "text/plain; charset=utf-8" );
mbp.setHeader("Content-Type","text/plain; charset=\"utf-8\"");
// System.out.println("Body (PJ) " + Body);
// Attach the part to the multipart
mp.addBodyPart(mbp);
}*/
File f = new File("/home/oracle/email_attach");
File[] attachments = f.listFiles();
for( int i = 0; i < attachments.length; i++ ) {
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attachments[i]);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mbp.setHeader("Content-Type", fds.getContentType());
mbp.setHeader("Content-ID", fds.getName());
mbp.setDisposition(Part.ATTACHMENT);
mp.addBodyPart(mbp);
}
/* // Attach the files to the message
if (null != Attachments) {
int StartIndex = 0, PosIndex = 0;
while (-1 != (PosIndex = Attachments.indexOf("///",StartIndex))) {
// Create and fill other message parts;
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(Attachments.substring(StartIndex,PosIndex));
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mbp.setHeader("Content-Type", fds.getContentType());
mbp.setHeader("Content-ID", fds.getName());
mbp.setDisposition(Part.INLINE);
mp.addBodyPart(mbp);
PosIndex += 3;
StartIndex = PosIndex;
}
// Last, or only, attachment file
if (StartIndex < Attachments.length()) {
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(Attachments.substring(StartIndex));
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mbp.setHeader("Content-Type", fds.getContentType());
mbp.setHeader("Content-ID", fds.getName());
mbp.setDisposition(Part.ATTACHMENT);
mp.addBodyPart(mbp);
}
}*/
// Add the Multipart to the message
msg.setContent(mp);
msg.setHeader("X-Mailer", "oracle.mail");
msg.setText(Body);
// set the Date: header
msg.setSentDate(new Date());
// Send the message
Transport.send(msg);
} catch (MessagingException MsgException) {
ErrorMessage[0] = MsgException.toString();
Exception TheException = null;
if ((TheException = MsgException.getNextException()) != null)
ErrorMessage[0] = ErrorMessage[0] + "\n" + TheException.toString();
ErrorStatus = 1;
}
return ErrorStatus;
} // End Send Class
} // End of public class SendMail
|
|
|