为什么应用程序崩溃,但电子邮件已发送给收件人?
使用 activation.jar , additional.jar 和 mail.jar 库
onPostExercute()
函数未执行
错误
java.lang.IllegalArgumentException:指定为非null的参数为null:方法kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数aVoid
class MainActivity : AppCompatActivity(), View.OnClickListener {
//Declaring EditText
private var editTextEmail: EditText? = null
private var editTextSubject: EditText? = null
private var editTextMessage: EditText? = null
//Send button
private var buttonSend: Button? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Initializing the views
editTextEmail = findViewById(R.id.editTextEmail)
editTextSubject = findViewById(R.id.editTextSubject)
editTextMessage = findViewById(R.id.editTextMessage)
buttonSend = findViewById(R.id.buttonSend) as Button
//Adding click listener
buttonSend!!.setOnClickListener(this)
}
private fun sendEmail() {
//Getting content for email
val email = editTextEmail!!.text.toString().trim { it <= ' ' }
val subject = editTextSubject!!.text.toString().trim { it <= ' ' }
val message = editTextMessage!!.text.toString().trim { it <= ' ' }
//Creating SendMail object
val sm = SendEmail(this, email, subject, message)
//Executing sendmail to send email
sm.execute()
}
override fun onClick(v: View) {
sendEmail()
}
}
class SendEmail//Class Constructor
(//Declaring Variables
private val context: Context, //Information to send email
private val email: String, private val subject: String, private val message: String
)//Initializing variables
: AsyncTask<Void, Void, Void>() {
private var session: Session? = null
//Progressdialog to show while sending email
private var progressDialog: ProgressDialog? = null
override fun onPreExecute() {
super.onPreExecute()
//Showing progress dialog while sending email
progressDialog = ProgressDialog.show(context, "Sending message", "Please wait...", false, false)
}
override fun onPostExecute(aVoid: Void) {
super.onPostExecute(aVoid)
//Dismissing the progress dialog
progressDialog!!.dismiss()
//Showing a success message
Toast.makeText(context, "Message Sent", Toast.LENGTH_LONG).show()
}
override fun doInBackground(vararg params: Void): Void? {
//Creating properties
val props = Properties()
//Configuring properties for gmail
//If you are not using gmail you may need to change the values
props.put("mail.smtp.host", "smtp.gmail.com")
props.put("mail.smtp.socketFactory.port", "465")
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory")
props.put("mail.smtp.auth", "true")
props.put("mail.smtp.port", "465")
//Creating a new session
session = Session.getDefaultInstance(props,
object : javax.mail.Authenticator() {
//Authenticating the password
override fun getPasswordAuthentication(): PasswordAuthentication {
return PasswordAuthentication(Config.EMAIL, Config.PASSWORD)
}
})
try {
//Creating MimeMessage object
val mm = MimeMessage(session)
//Setting sender address
mm.setFrom(InternetAddress(Config.EMAIL))
//Adding receiver
mm.addRecipient(Message.RecipientType.TO, InternetAddress(email))
//Adding subject
mm.setSubject(subject)
//Adding message
mm.setText(message)
//Sending email
Transport.send(mm)
Toast.makeText(context, "success", Toast.LENGTH_LONG).show()
//Toast.makeText(this, "message", Toast.LENGTH_SHORT).show()
} catch (e: MessagingException) {
e.printStackTrace()
}
return null
}
}
答案 0 :(得分:0)
更改
override fun onPostExecute(aVoid: Void) {
收件人:
override fun onPostExecute(aVoid: Void?) {
当您从doInBackground
返回null并传递给onPostExecute