Java 文件未附加到电子邮件
我有以下问题,我正在尝试将文件附加到正在发送的邮件,但它没有附加到邮件 有关邮件,请参见下面的代码:Java 文件未附加到电子邮件,java,jakarta-mail,Java,Jakarta Mail,我有以下问题,我正在尝试将文件附加到正在发送的邮件,但它没有附加到邮件 有关邮件,请参见下面的代码: public static boolean sendEmail(String subject, String emailMessage, String from, String to[],String[] attachFiles ) throws MessagingException, IOException { boolean result = true; try {
public static boolean sendEmail(String subject, String emailMessage, String from, String to[],String[] attachFiles ) throws MessagingException, IOException {
boolean result = true;
try {
final String username = "nmtsystem@gmail.com";
final String password = "nMT@DMIn!";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for (int i = 0; i < to.length; i++) { // changed from a while loop
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(javax.mail.Message.RecipientType.TO, toAddress[i]);
}
public static boolean sendmail(字符串主题、字符串emailMessage、字符串发件人、字符串收件人[]、字符串[]附件)抛出MessaginException、IOException{
布尔结果=真;
试一试{
最终字符串用户名=”nmtsystem@gmail.com";
最终字符串密码=”nMT@DMIn!";
Properties props=新属性();
props.put(“mail.smtp.auth”,“true”);
props.put(“mail.smtp.starttls.enable”、“true”);
put(“mail.smtp.host”、“smtp.gmail.com”);
props.put(“mail.smtp.port”,“587”);
会话=会话.getInstance(props,
新的javax.mail.Authenticator(){
@凌驾
受保护的密码身份验证getPasswordAuthentication(){
返回新密码身份验证(用户名、密码);
}
});
MimeMessage message=新MimeMessage(会话);
message.setFrom(新的InternetAddress(from));
InternetAddress[]toAddress=新的InternetAddress[to.length];
//获取地址数组的步骤
对于(int i=0;i
//创建消息部分
MimeBodyPart messageBodyPart=新的MimeBodyPart()
//创建多个部件
Multipart Multipart=新的MimeMultipart();
multipart.addBodyPart(messageBodyPart);
//添加附件
if(attachFiles!=null&&attachFiles.length>0){
for(字符串文件路径:attachFiles){
MimeBodyPart attachPart=新的MimeBodyPart();
试一试{
attachPart.attachFile(文件路径);
}捕获(IOEX异常){
例如printStackTrace();
}
多部件添加车身部件(附件部件);
}
}
message.setSubject(主题);
message.setContent(“”+emailMessage+“”,“text/html”);
传输。发送(消息);
}捕获(消息例外){
结果=假;
掷骰子;
}
返回结果;
}
这是邮件正文的代码:
public void queryMailPayAt() {
String sql = "select caseNumber from tblpay@qlheader where transactionid = " + selectedQuery.getRecId();
String caseId = "";
try {
Connection con = DatabaseHandler.getDBConnection(DatabaseTypes.TRANSACTION_DATABASE);
ResultSet result = DatabaseHandler.executeQuery(con, sql);
while (result.next()) {
caseId = result.getString("caseNumber");
}
} catch (Exception e) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "An error occurred.", e.getMessage()));
String ex = e.getMessage();
String ex1 = e.getLocalizedMessage();
System.out.println(ex + " " + ex1);
}
String from = "admin@nmtservice.co.za";
String style = "<font size=\"-1\" face=\"Verdana, Arial, Helvetica, sans-serif\">";
String msgPrefix = "<h4>New B.I.N has been logged</h4>";
String msg = "<p>See details for newly logged B.I.N.</p>"
+ "<p>Issue Case ID: " + caseId + "</p>"
+ "<table><tbody>"
+ "<tr><td>" + style + "Username: </font></td><td>" + style + user.getUsername() + "</font></td></tr>"
+ "<tr><td>" + style + "First Name: </font></td><td>" + style + user.getFirstName() + "</font></td></tr>"
+ "<tr><td>" + style + "Last Name: </font></td><td>" + style + user.getLastName() + "</font></td></tr>"
+ "<tr><td>" + style + "Transaction ID: </font></td><td>" + style + selectedQuery.getRecId() + "</font></td></tr>"
+ "</tbody></table>"
+ "<p> If this is a valid query please confirm on the Open queries page </p>"
+ "<p>Please contact <a href=\"mailto:support@payat-it.co.za\">Pay@ Support</a> for any further assistance.</p>";
String emailMessage = msgPrefix + msg;
String subject = "New Logged B.I.N: " + caseId;
String[] attachFiles = new String[1];
attachFiles[0] = "C:\\Users\\kroon_000\\Desktop\\files\\"+ caseId + ".pdf" ;
try {
String[] to = {"dirk@nmt-it.co.za", user.getEmailAddress() };
Utils.sendEmail(subject, emailMessage, from, to, attachFiles);
} catch (Exception ex) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "An error occurred.", "Please try again."));
}
}
public void queryMailPayAt(){
String sql=“从中选择案例编号”tblpay@qlheader其中transactionid=“+selectedQuery.getRecId();
字符串caseId=“”;
试一试{
Connection con=DatabaseHandler.getDBConnection(DatabaseTypes.TRANSACTION\u数据库);
ResultSet result=DatabaseHandler.executeQuery(con,sql);
while(result.next()){
caseId=result.getString(“caseNumber”);
}
}捕获(例外e){
FacesContext.getCurrentInstance().addMessage(空,新的FacesMessage(FacesMessage.SEVERITY_错误,“发生错误”,e.getMessage());
字符串ex=e.getMessage();
字符串ex1=e.getLocalizedMessage();
System.out.println(ex+“”+ex1);
}
字符串from=”admin@nmtservice.co.za";
字符串样式=”;
字符串msgPrefix=“已记录新的B.I.N”;
String msg=“有关新记录的B.I.N.的详细信息,请参见”
+问题案例ID:“+caseId+””
+ ""
+“+style+”用户名:“+style+user.getUsername()+”
+“+style+”名字:“+style+user.getFirstName()+”
+“+style+”姓氏:“+style+user.getLastName()+”
+“+style+”事务ID:“+style+selectedQuery.getRecId()+”
+ ""
+“如果这是一个有效的查询,请在“打开查询”页面上确认”
+“如需进一步帮助,请联系。”;
字符串emailMessage=msgPrefix+msg;
字符串主题=“新记录的B.I.N:”+案例ID;
String[]attachFiles=新字符串[1];
附件文件[0]=“C:\\Users\\kroon\u 000\\Desktop\\files\\\”+caseId+“.pdf”;
试一试{
字符串[]到={”dirk@nmt-it.co.za“,user.getEmailAddress()};
Utils.sendmail(主题、emailMessage、发件人、收件人、附件);
}捕获(例外情况除外){
FacesContext.getCurrentInstance().addMessage(空,新的FacesMessage(FacesMessage.SEVERITY_错误,“发生错误”,“请重试”);
}
}
如果有人能在我做错的地方帮助我,我将不胜感激。在您的代码中,
multipart
已创建但未使用。您应该添加:
message.setContent(multipart);
谢谢磨坊,真不敢相信我错过了这么小的一件事……哈哈
message.setContent(multipart);