无法同时发送短信和附件

时间:2015-06-08 07:51:32

标签: java javamail javax.activation

添加文档文件后,我无法发送消息。

在代码msg.setText()中添加public static void sendTo(String seniorId,String seniorName){ final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; // Get a Properties object Properties props = System.getProperties(); props.setProperty("mail.smtp.host", "smtp.gmail.com"); props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.port", "465"); props.setProperty("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.auth", "true"); props.put("mail.debug", "true"); props.put("mail.store.protocol", "pop3"); props.put("mail.transport.protocol", "smtp"); final String username = "rptdby@gmail.com";// final String password = "xxxxxxxxxxxxxxxx"; try{ Session session = Session.getDefaultInstance(props, new Authenticator(){ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); }}); // -- Create a new message -- Message msg = new MimeMessage(session); // -- Set the FROM and TO fields -- msg.setFrom(new InternetAddress("rptdby@gmail.com")); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(seniorId,false)); msg.setSubject("Suject"); msg.setText("Hi "+seniorName+"Sir"+"\n\nI am in India\nplease find my attached FILE.\n\nthanks\n\ndubey-theHarcourtian"); String filename = "C:\\Users\\arpit.dubey\\Desktop\\sysofnI\\Myfile.docx"; DataSource source = new FileDataSource(filename); msg.setDataHandler(new DataHandler(source)); msg.setFileName("MyFile"); msg.setSentDate(new Date()); Transport.send(msg); System.out.println("Message sent."); }catch (MessagingException e){ System.out.println("Erreur d'envoi, cause: " + e);} } 后无效。

正在使用附件成功传递邮件,但邮件正文中没有文本。 无法同时发送短信和附件。

下面是我的代码文件 -

<?php

class myclass {
    static function say_hello()
    {
        echo "Hello!\n";
    }
}

$classname = "myclass";

call_user_func(array($classname, 'say_hello'));
call_user_func($classname .'::say_hello'); // As of 5.2.3

$myobject = new myclass();

call_user_func(array($myobject, 'say_hello'));

?>

1 个答案:

答案 0 :(得分:0)

您需要将文件添加到正文部分并将其添加到multipart。我们不应该直接将它添加到邮件头。          消息消息=新的MimeMessage(会话);          message.setFrom(new InternetAddress(from));          message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(至));          message.setSubject(&#34; Testing Subject&#34;);          BodyPart messageBodyPart = new MimeBodyPart();          messageBodyPart.setText(&#34;这是消息体&#34;);          Multipart multipart = new MimeMultipart();          multipart.addBodyPart(messageBodyPart);          messageBodyPart = new MimeBodyPart();          String filename =&#34; /Mydocuments/kali/file.txt" ;;          DataSource source = new FileDataSource(filename);          messageBodyPart.setDataHandler(new DataHandler(source));          messageBodyPart.setFileName(文件名);          multipart.addBodyPart(messageBodyPart);          message.setContent(多部分);          Transport.send(消息);          System.out.println(&#34;已成功发送消息....&#34;);