简单的邮件发送窗体,托管c++版:
添加空间引用:using namespace System::Net::Mail; using namespace System::Net::NetworkInformation; using namespace System::Net;
添加textbox和按钮。
代码:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { String ^sm; MailMessage ^aMessage=gcnew MailMessage(); if (textBox1->Text=="") { MessageBox::Show("填写地址");
} else if(textBox2->Text=="") { MessageBox::Show("xiejiandizhi");
} else {
try{ aMessage = gcnew MailMessage(textBox2->Text->Trim(),textBox1->Text->Trim()); sm= this->textBox2->Text; int s=sm->IndexOf('@'); int l=sm->IndexOf('.'); String ^sep=sm->Substring(s+1,l-s-1); //组合SMTP服务器地址 String ^site = "smtp." + sep + ".com"; SmtpClient ^client = gcnew SmtpClient(site); client->UseDefaultCredentials = false; NetworkCredential ^myCredential=gcnew NetworkCredential(textBox2->Text->Trim(),textBox3->Text); client->Credentials = myCredential; client->DeliveryMethod = SmtpDeliveryMethod::Network;
//邮件主题 aMessage->Subject =textBox4->Text; //邮件内容 aMessage->Body =textBox5->Text; //发送电子邮件 client->Send(aMessage); MessageBox::Show("邮件发送成功!"); } catch(Exception ^ex) { MessageBox::Show(ex->Message); } }
注释:textbox1为收件人地址,box2为写件人地址,box为写件人地址密码,box4为邮件主题,box5为邮件内容。
|
请发表评论