0
public void sendEMailThroughOUTLOOK()
{
if(MessageBox.Show(PublicFunction.L_Get_Msg("msg",28),"MessageBox",MessageBoxButtons.OKCancel)== DialogResult.OK)
{
try
{
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
//set default account send mail
Outlook.Account account = GetAccountForEmailAddress(oApp,"[email protected]");
// Create a new mail item.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
// Set HTMLBody.
//oMsg.HTMLBody= "<html> <p style=font size:200%>"+PublicFunction.L_Get_Msg("msg",29)+"<br/>"+ PublicFunction.L_Get_Msg("msg",30)+"</p> </html>";
oMsg.HTMLBody= txtSubject.Text;
if(txtSubject.Text.Length < 1)
{
MessageBox.Show(PublicFunction.L_Get_Msg("msg",29));
return;
}
//Add an attachment.
String sDisplayName = "MyAttachment";
int iPosition = (int)oMsg.Body.Length + 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
//now attached the file
string fileName = Application.StartupPath + @"\\Reports\\RPTTienDoCongViec.xls";
Outlook.Attachment oAttach = oMsg.Attachments.Add(fileName, iAttachType, iPosition, sDisplayName);
oMsg.Subject = PublicFunction.L_Get_Msg("msg",34)+ DateTime.Now.ToString("yyyy-MM-dd");
// Add a recipient
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
char [] catkytu = {','};
string ThoaiSendEmail ="";
string [] mailsend = txtEmail.Text.Split(catkytu);
for(int Thoaiitem = 0; Thoaiitem < mailsend.Length;Thoaiitem++)
{
ThoaiSendEmail = mailsend[Thoaiitem].ToString();
foreach (string recipient in new string[] {ThoaiSendEmail})
{
Microsoft.Office.Interop.Outlook.Recipient oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(recipient);
oRecip.Resolve();
}
}
oMsg.SendUsingAccount = account;
oMsg.Send();
oRecips = null;
oMsg = null;
oApp = null;
MessageBox.Show(PublicFunction.L_Get_Msg("msg",32));
}
catch
{
MessageBox.Show(PublicFunction.L_Get_Msg("msg",33));
}
}
else return;
}
public void sendEMailMTNLLD()
{
try
{
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace NS = oApp.GetNamespace("MAPI");
NS.Logon("[email protected]","pass",true,true);
// Create a new mail item.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
// Set HTMLBody.
oMsg.HTMLBody= "<html> <p style=font size:200%>Dear Patrick<br/>"+PublicFunction.L_Get_Msg("msg",30)+"</p> </html>";
//Add an attachment.
String sDisplayName = "";
int iPosition = (int)oMsg.Body.Length + 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
//now attached the file
string fileName = Application.StartupPath + @"\\Reports\\RPTTienDoCongViec.xls";
Outlook.Attachment oAttach = oMsg.Attachments.Add(fileName, iAttachType, iPosition, sDisplayName);
oMsg.Subject = "Dear Patrick";
// Add a recipient
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
foreach (string recipient in new string[] {"[email protected]"/*"[email protected]"*/})
{
Microsoft.Office.Interop.Outlook.Recipient oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(recipient);
oRecip.Resolve();
}
Outlook.Account account = GetAccountForEmailAddress(oApp,"[email protected]");
oMsg.SendUsingAccount = account;
oMsg.Send();
oRecips = null;
oMsg = null;
oApp = null;
NS = null;
}//end of try block
catch
{
MessageBox.Show(PublicFunction.L_Get_Msg("msg",33));
}
}
public static Outlook.Account GetAccountForEmailAddress(Outlook.Application oApp, string smtpAddress)
{
Outlook.Accounts accounts = oApp.Session.Accounts;
foreach (Outlook.Account account in accounts)
{
if (account.SmtpAddress == smtpAddress)
{
return account;
}
}
throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress));
}
你有什麼樣的o帳戶? POP3/SMTP?代表Exchange賬戶?太平洋標準時間? –