2016-10-25 3 views
1

smtp를 통해 전자 메일을 보내고 있지만 전자 메일 및 제목의 본문이 아닌 파일 또는 첨부 파일 만 수신합니다. 아무도 문제가 어디 있는지 말해 줄 수 있습니까?이메일 본문 및 본문없이 전자 메일을 수신합니다.

Public 클래스 센드 AppCompatActivity는 {

EditText edt_subject,edt_body,edt_choosenFile; 
private Button sendEmail; 
private Button chooseFileButton; 
private String filename; 
String uniqueId,subject,mailbody; 
private static final int REQUEST_CHOOSER = 1234; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_send_mail); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    edt_subject=(EditText)findViewById(R.id.et_Subject); 
    edt_body=(EditText)findViewById(R.id.et_mail_body); 
    subject=edt_subject.getText().toString(); 
    mailbody=edt_body.getText().toString(); 

    sendEmail = (Button) findViewById(R.id.send_email_button_id); 
    sendEmail.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      new Thread(new Runnable() { 

       @Override 
       public void run() { 
        try { 
         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 Authenticator() { 
            protected PasswordAuthentication getPasswordAuthentication() { 
             return new PasswordAuthentication(
               "[email protected]","password"); 
            } 
           }); 
         // TODO Auto-generated method stub 
         MimeMessage message = new MimeMessage(session); 
         message.setFrom(new InternetAddress("[email protected]")); 
         message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]")); 
         message.setSubject(subject); 
        message.setText(mailbody); 

         if (filename!=null) { 

          BodyPart messageBodyPart = new MimeBodyPart(); 
          messageBodyPart.setText(mailbody); 
          Multipart _multipart = new MimeMultipart(); 
          FileDataSource source = new FileDataSource(filename); 
          messageBodyPart.setDataHandler(new DataHandler(source)); 
          messageBodyPart.setFileName(filename); 


          _multipart.addBodyPart(messageBodyPart); 
          message.setContent(_multipart); 
         } 

         Transport.send(message); 
         System.out.println("Done"); 
        } catch (MessagingException e) { 
         throw new RuntimeException(e); 
        } 
       } 
      }).start(); 
     } 
    }); 

    chooseFileButton = (Button) findViewById(R.id.choose_file_button_id); 
    chooseFileButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // Create the ACTION_GET_CONTENT Intent 
      Intent getContentIntent = FileUtils.createGetContentIntent(); 

      Intent intent = Intent.createChooser(getContentIntent, "Select a file"); 
      startActivityForResult(intent, REQUEST_CHOOSER); 
     } 
    }); 
} 

답변

0

본체 부분이 다중의 첫번째 제 부위 있어야 연장된다. message.setText에 대한 호출은 message.setContent를 호출하여 덮어 씁니다. JavaMail sample programs에는 많은 예제가 있습니다.

관련 문제